Ticket #6398: add_default_to_for.3.diff
File add_default_to_for.3.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 self.nodelist_loop = nodelist_loop90 self.nodelist_loop, self.nodelist_default = nodelist_loop, nodelist_default 91 91 92 92 def __repr__(self): 93 93 reversed_text = self.is_reversed and ' reversed' or '' … … 98 98 def __iter__(self): 99 99 for node in self.nodelist_loop: 100 100 yield node 101 for node in self.nodelist_default: 102 yield node 101 103 102 104 def get_nodes_by_type(self, nodetype): 103 105 nodes = [] 104 106 if isinstance(self, nodetype): 105 107 nodes.append(self) 106 108 nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype)) 109 nodes.extend(self.nodelist_default.get_nodes_by_type(nodetype)) 107 110 return nodes 108 111 109 112 def render(self, context): 110 nodelist = NodeList()111 113 if 'forloop' in context: 112 114 parentloop = context['forloop'] 113 115 else: … … 122 124 if not hasattr(values, '__len__'): 123 125 values = list(values) 124 126 len_values = len(values) 127 if len_values < 1: 128 return self.nodelist_default.render(context) 129 nodelist = NodeList() 125 130 if self.is_reversed: 126 131 values = reversed(values) 127 132 unpack = len(self.loopvars) > 1 … … 594 599 {{ key }}: {{ value }} 595 600 {% endfor %} 596 601 602 As you can see, the ``for`` tag can take an option ``{% default %}`` clause 603 that will be displayed if the given array is empty or could not be found:: 604 605 <ul> 606 {% for athlete in athlete_list %} 607 <li>{{ athlete.name }}</li> 608 {% default %} 609 <li>Sorry, no athlete in this list!</li> 610 {% endfor %} 611 <ul> 612 597 613 The for loop sets a number of variables available within the loop: 598 614 599 615 ========================== ================================================ … … 630 646 " %s" % token.contents) 631 647 632 648 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) 649 nodelist_loop = parser.parse(('default', 'endfor',)) 650 token = parser.next_token() 651 if token.contents == 'default': 652 nodelist_default = parser.parse(('endfor',)) 653 parser.delete_first_token() 654 else: 655 nodelist_default = NodeList() 656 return ForNode(loopvars, sequence, is_reversed, nodelist_loop, nodelist_default) 636 657 do_for = register.tag("for", do_for) 637 658 638 659 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
700 700 You can loop over a list in reverse by using ``{% for obj in list reversed %}``. 701 701 702 702 **New in Django development version** 703 703 704 If you need to loop over a list of lists, you can unpack the values 704 705 in eachs sub-list into a set of known names. For example, if your context contains 705 706 a list of (x,y) coordinates called ``points``, you could use the following … … 717 718 {{ key }}: {{ value }} 718 719 {% endfor %} 719 720 721 **New in Django development version** 722 723 The ``for`` tag can take an optional ``{% default %}`` clause that will be 724 displayed if the given array is empty or could not be found:: 725 726 <ul> 727 {% for athlete in athlete_list %} 728 <li>{{ athlete.name }}</li> 729 {% default %} 730 <li>Sorry, no athlete in this list!</li> 731 {% endfor %} 732 <ul> 733 720 734 The for loop sets a number of variables available within the loop: 721 735 722 736 ========================== ================================================