Ticket #6398: add_default_to_for.4.diff
File add_default_to_for.4.diff, 5.6 KB (added by , 17 years ago) |
---|
-
django/template/defaulttags.py
84 84 return u'' 85 85 86 86 class ForNode(Node): 87 def __init__(self, loopvars, sequence, is_reversed, nodelist_loop ):87 def __init__(self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_default=None): 88 88 self.loopvars, self.sequence = loopvars, sequence 89 89 self.is_reversed = is_reversed 90 90 self.nodelist_loop = nodelist_loop 91 if nodelist_default is None: 92 self.nodelist_default = NodeList() 93 else: 94 self.nodelist_default = nodelist_default 91 95 92 96 def __repr__(self): 93 97 reversed_text = self.is_reversed and ' reversed' or '' … … 98 102 def __iter__(self): 99 103 for node in self.nodelist_loop: 100 104 yield node 105 for node in self.nodelist_default: 106 yield node 101 107 102 108 def get_nodes_by_type(self, nodetype): 103 109 nodes = [] 104 110 if isinstance(self, nodetype): 105 111 nodes.append(self) 106 112 nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype)) 113 nodes.extend(self.nodelist_default.get_nodes_by_type(nodetype)) 107 114 return nodes 108 115 109 116 def render(self, context): 110 nodelist = NodeList()111 117 if 'forloop' in context: 112 118 parentloop = context['forloop'] 113 119 else: … … 122 128 if not hasattr(values, '__len__'): 123 129 values = list(values) 124 130 len_values = len(values) 131 if len_values < 1: 132 return self.nodelist_default.render(context) 133 nodelist = NodeList() 125 134 if self.is_reversed: 126 135 values = reversed(values) 127 136 unpack = len(self.loopvars) > 1 … … 594 603 {{ key }}: {{ value }} 595 604 {% endfor %} 596 605 606 The ``for`` tag can take an optional ``{% default %}`` clause that will 607 be displayed if the given array is empty or could not be found:: 608 609 <ul> 610 {% for athlete in athlete_list %} 611 <li>{{ athlete.name }}</li> 612 {% default %} 613 <li>Sorry, no athlete in this list!</li> 614 {% endfor %} 615 <ul> 616 597 617 The for loop sets a number of variables available within the loop: 598 618 599 619 ========================== ================================================ … … 630 650 " %s" % token.contents) 631 651 632 652 sequence = parser.compile_filter(bits[in_index+1]) 633 nodelist_loop = parser.parse(('endfor',)) 634 parser.delete_first_token() 635 return ForNode(loopvars, sequence, is_reversed, nodelist_loop) 653 nodelist_loop = parser.parse(('default', 'endfor',)) 654 token = parser.next_token() 655 if token.contents == 'default': 656 nodelist_default = parser.parse(('endfor',)) 657 parser.delete_first_token() 658 else: 659 nodelist_default = None 660 return ForNode(loopvars, sequence, is_reversed, nodelist_loop, nodelist_default) 636 661 do_for = register.tag("for", do_for) 637 662 638 663 def do_ifequal(parser, token, negate): -
tests/regressiontests/templates/tests.py
457 457 '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/")), 458 458 '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/")), 459 459 '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/")), 460 'for-tag-default01': ("{% for val in values %}{{ val }}{% default %}default text{% endfor %}", {"values": [1, 2, 3]}, "123"), 461 'for-tag-default02': ("{% for val in values %}{{ val }}{% default %}values array empty{% endfor %}", {"values": []}, "values array empty"), 462 'for-tag-default03': ("{% for val in values %}{{ val }}{% default %}values array not found{% endfor %}", {}, "values array not found"), 460 463 461 464 ### IF TAG ################################################################ 462 465 'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"), -
docs/templates.txt
702 702 You can loop over a list in reverse by using ``{% for obj in list reversed %}``. 703 703 704 704 **New in Django development version** 705 705 706 If you need to loop over a list of lists, you can unpack the values 706 707 in eachs sub-list into a set of known names. For example, if your context contains 707 708 a list of (x,y) coordinates called ``points``, you could use the following … … 719 720 {{ key }}: {{ value }} 720 721 {% endfor %} 721 722 723 **New in Django development version** 724 725 The ``for`` tag can take an optional ``{% default %}`` clause that will be 726 displayed if the given array is empty or could not be found:: 727 728 <ul> 729 {% for athlete in athlete_list %} 730 <li>{{ athlete.name }}</li> 731 {% default %} 732 <li>Sorry, no athlete in this list!</li> 733 {% endfor %} 734 <ul> 735 722 736 The for loop sets a number of variables available within the loop: 723 737 724 738 ========================== ================================================