Ticket #6398: ticket6398-r9032.diff
File ticket6398-r9032.diff, 5.4 KB (added by , 16 years ago) |
---|
-
django/template/defaulttags.py
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 8182cd4..22777b7 100644
a b class FirstOfNode(Node): 83 83 return u'' 84 84 85 85 class ForNode(Node): 86 def __init__(self, loopvars, sequence, is_reversed, nodelist_loop ):86 def __init__(self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_default=None): 87 87 self.loopvars, self.sequence = loopvars, sequence 88 88 self.is_reversed = is_reversed 89 89 self.nodelist_loop = nodelist_loop 90 if nodelist_default is None: 91 self.nodelist_default = NodeList() 92 else: 93 self.nodelist_default = nodelist_default 90 94 91 95 def __repr__(self): 92 96 reversed_text = self.is_reversed and ' reversed' or '' … … class ForNode(Node): 97 101 def __iter__(self): 98 102 for node in self.nodelist_loop: 99 103 yield node 104 for node in self.nodelist_default: 105 yield node 100 106 101 107 def get_nodes_by_type(self, nodetype): 102 108 nodes = [] 103 109 if isinstance(self, nodetype): 104 110 nodes.append(self) 105 111 nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype)) 112 nodes.extend(self.nodelist_default.get_nodes_by_type(nodetype)) 106 113 return nodes 107 114 108 115 def render(self, context): 109 nodelist = NodeList()110 116 if 'forloop' in context: 111 117 parentloop = context['forloop'] 112 118 else: … … class ForNode(Node): 121 127 if not hasattr(values, '__len__'): 122 128 values = list(values) 123 129 len_values = len(values) 130 if len_values < 1: 131 return self.nodelist_default.render(context) 132 nodelist = NodeList() 124 133 if self.is_reversed: 125 134 values = reversed(values) 126 135 unpack = len(self.loopvars) > 1 … … def do_for(parser, token): 610 619 {{ key }}: {{ value }} 611 620 {% endfor %} 612 621 622 The ``for`` tag can take an optional ``{% default %}`` clause that will 623 be displayed if the given array is empty or could not be found:: 624 625 <ul> 626 {% for athlete in athlete_list %} 627 <li>{{ athlete.name }}</li> 628 {% default %} 629 <li>Sorry, no athlete in this list!</li> 630 {% endfor %} 631 <ul> 632 613 633 The for loop sets a number of variables available within the loop: 614 634 615 635 ========================== ================================================ … … def do_for(parser, token): 646 666 " %s" % token.contents) 647 667 648 668 sequence = parser.compile_filter(bits[in_index+1]) 649 nodelist_loop = parser.parse(('endfor',)) 650 parser.delete_first_token() 651 return ForNode(loopvars, sequence, is_reversed, nodelist_loop) 669 nodelist_loop = parser.parse(('default', 'endfor',)) 670 token = parser.next_token() 671 if token.contents == 'default': 672 nodelist_default = parser.parse(('endfor',)) 673 parser.delete_first_token() 674 else: 675 nodelist_default = None 676 return ForNode(loopvars, sequence, is_reversed, nodelist_loop, nodelist_default) 652 677 do_for = register.tag("for", do_for) 653 678 654 679 def do_ifequal(parser, token, negate): -
docs/ref/templates/builtins.txt
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index a28cf4f..f2c6953 100644
a b would display the keys and values of the dictionary:: 192 192 {{ key }}: {{ value }} 193 193 {% endfor %} 194 194 195 .. versionadded:: 1.1 196 197 The ``for`` tag can take an optional ``{% default %}`` clause that will be 198 displayed if the given array is empty or could not be found:: 199 200 <ul> 201 {% for athlete in athlete_list %} 202 <li>{{ athlete.name }}</li> 203 {% default %} 204 <li>Sorry, no athlete in this list!</li> 205 {% endfor %} 206 <ul> 207 195 208 The for loop sets a number of variables available within the loop: 196 209 197 210 ========================== ================================================ -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index c69e7cd..6d728e9 100644
a b class Templates(unittest.TestCase): 473 473 'for-tag-unpack11': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, ("one:1,/two:2,/", "one:1,INVALID/two:2,INVALID/")), 474 474 'for-tag-unpack12': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2))}, ("one:1,carrot/two:2,/", "one:1,carrot/two:2,INVALID/")), 475 475 'for-tag-unpack13': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'cheese'))}, ("one:1,carrot/two:2,cheese/", "one:1,carrot/two:2,cheese/")), 476 'for-tag-default01': ("{% for val in values %}{{ val }}{% default %}default text{% endfor %}", {"values": [1, 2, 3]}, "123"), 477 'for-tag-default02': ("{% for val in values %}{{ val }}{% default %}values array empty{% endfor %}", {"values": []}, "values array empty"), 478 'for-tag-default03': ("{% for val in values %}{{ val }}{% default %}values array not found{% endfor %}", {}, "values array not found"), 476 479 477 480 ### IF TAG ################################################################ 478 481 'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),