Ticket #4534: ifchanged-else.patch
File ifchanged-else.patch, 4.6 KB (added by , 17 years ago) |
---|
-
django/template/defaulttags.py
127 127 return nodelist.render(context) 128 128 129 129 class IfChangedNode(Node): 130 def __init__(self, nodelist , *varlist):131 self.nodelist = nodelist130 def __init__(self, nodelist_true, nodelist_false, *varlist): 131 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 132 132 self._last_seen = None 133 133 self._varlist = varlist 134 134 … … 141 141 # This automatically behaves like a OR evaluation of the multiple variables. 142 142 compare_to = [resolve_variable(var, context) for var in self._varlist] 143 143 else: 144 compare_to = self.nodelist .render(context)144 compare_to = self.nodelist_true.render(context) 145 145 except VariableDoesNotExist: 146 146 compare_to = None 147 147 … … 150 150 self._last_seen = compare_to 151 151 context.push() 152 152 context['ifchanged'] = {'firstloop': firstloop} 153 content = self.nodelist .render(context)153 content = self.nodelist_true.render(context) 154 154 context.pop() 155 155 return content 156 156 else: 157 return '' 157 if self.nodelist_false: 158 return self.nodelist_false.render(context) 159 else: 160 return '' 158 161 159 162 class IfEqualNode(Node): 160 163 def __init__(self, var1, var2, nodelist_true, nodelist_false, negate): … … 718 721 {% endfor %} 719 722 """ 720 723 bits = token.contents.split() 721 nodelist = parser.parse(('endifchanged',)) 722 parser.delete_first_token() 723 return IfChangedNode(nodelist, *bits[1:]) 724 nodelist_true = parser.parse(('else', 'endifchanged')) 725 token = parser.next_token() 726 if token.contents == 'else': 727 nodelist_false = parser.parse(('endifchanged',)) 728 parser.delete_first_token() 729 else: 730 nodelist_false = NodeList() 731 return IfChangedNode(nodelist_true, nodelist_false, *bits[1:]) 724 732 ifchanged = register.tag(ifchanged) 725 733 726 734 #@register.tag -
docs/templates.txt
566 566 {% endifchanged %} 567 567 {% endfor %} 568 568 569 The ``ifchanged`` tag can also take an optional ``{% else %}`` clause that 570 will be displayed if the value has not changed: 571 572 {% for match in matches %} 573 <div style="background-color: 574 {% ifchanged match.ballot_id %} 575 {% cycle red,blue %} 576 {% else %} 577 grey 578 {% endifchanged %} 579 ">{{ match }}</div> 580 {% endfor %} 581 569 582 ifequal 570 583 ~~~~~~~ 571 584 -
tests/regressiontests/templates/tests.py
391 391 # ifchanged for the day. 392 392 'ifchanged-param04': ('{% for d in days %}{% ifchanged d.day %}{{ d.day }}{% endifchanged %}{% for h in d.hours %}{% ifchanged d.day h %}{{ h }}{% endifchanged %}{% endfor %}{% endfor %}', {'days':[{'day':1, 'hours':[1,2,3]},{'day':2, 'hours':[3]},] }, '112323'), 393 393 394 # Test the else clause of ifchanged. 395 'ifchanged-else01': ('{% for id in ids %}{{ id }}{% ifchanged id %}-first{% else %}-other{% endifchanged %},{% endfor %}', {'ids': [1,1,2,2,2,3]}, '1-first,1-other,2-first,2-other,2-other,3-first,'), 396 397 'ifchanged-else02': ('{% for id in ids %}{{ id }}-{% ifchanged id %}{% cycle red,blue %}{% else %}grey{% endifchanged %},{% endfor %}', {'ids': [1,1,2,2,2,3]}, '1-red,1-grey,2-blue,2-grey,2-grey,3-red,'), 398 'ifchanged-else03': ('{% for id in ids %}{{ id }}{% ifchanged id %}-{% cycle red,blue %}{% else %}{% endifchanged %},{% endfor %}', {'ids': [1,1,2,2,2,3]}, '1-red,1,2-blue,2,2,3-red,'), 399 400 'ifchanged-else04': ('{% for id in ids %}{% ifchanged %}***{{ id }}*{% else %}...{% endifchanged %}{{ forloop.counter }}{% endfor %}', {'ids': [1,1,2,2,2,3,4]}, '***1*1...2***2*3...4...5***3*6***4*7'), 401 394 402 ### IFEQUAL TAG ########################################################### 395 403 'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""), 396 404 'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),