Changeset 8095
- Timestamp:
- 07/26/08 17:09:43 (4 months ago)
- Files:
-
- django/trunk/django/template/defaulttags.py (modified) (3 diffs)
- django/trunk/docs/templates.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/template/defaulttags.py
r7758 r8095 158 158 159 159 class IfChangedNode(Node): 160 def __init__(self, nodelist , *varlist):161 self.nodelist = nodelist160 def __init__(self, nodelist_true, nodelist_false, *varlist): 161 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 162 162 self._last_seen = None 163 163 self._varlist = map(Variable, varlist) … … 174 174 compare_to = [var.resolve(context) for var in self._varlist] 175 175 else: 176 compare_to = self.nodelist .render(context)176 compare_to = self.nodelist_true.render(context) 177 177 except VariableDoesNotExist: 178 178 compare_to = None 179 179 180 if compare_to != self._last_seen:180 if compare_to != self._last_seen: 181 181 firstloop = (self._last_seen == None) 182 182 self._last_seen = compare_to 183 183 context.push() 184 184 context['ifchanged'] = {'firstloop': firstloop} 185 content = self.nodelist .render(context)185 content = self.nodelist_true.render(context) 186 186 context.pop() 187 187 return content 188 else: 189 return '' 188 elif self.nodelist_false: 189 return self.nodelist_false.render(context) 190 return '' 190 191 191 192 class IfEqualNode(Node): … … 804 805 """ 805 806 bits = token.contents.split() 806 nodelist = parser.parse(('endifchanged',)) 807 parser.delete_first_token() 808 return IfChangedNode(nodelist, *bits[1:]) 807 nodelist_true = parser.parse(('else', 'endifchanged')) 808 token = parser.next_token() 809 if token.contents == 'else': 810 nodelist_false = parser.parse(('endifchanged',)) 811 parser.delete_first_token() 812 else: 813 nodelist_false = NodeList() 814 return IfChangedNode(nodelist_true, nodelist_false, *bits[1:]) 809 815 ifchanged = register.tag(ifchanged) 810 816 django/trunk/docs/templates.txt
r8081 r8095 828 828 {% endifchanged %} 829 829 {% endfor %} 830 831 The ``ifchanged`` tag also takes an optional ``{% else %}`` clause that will 832 be displayed if the value has not changed:: 833 834 {% for match in matches %} 835 <div style="background-color: 836 {% ifchanged match.ballot_id %} 837 {% cycle red,blue %} 838 {% else %} 839 grey 840 {% endifchanged %} 841 ">{{ match }}</div> 842 {% endfor %} 830 843 831 844 ifequal django/trunk/tests/regressiontests/templates/tests.py
r7754 r8095 566 566 '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'), 567 567 568 # Test the else clause of ifchanged. 569 '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,'), 570 571 '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,'), 572 '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,'), 573 574 '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'), 575 568 576 ### IFEQUAL TAG ########################################################### 569 577 'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""), … … 713 721 # Inheritance from a template that doesn't have any blocks 714 722 'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'), 715 723 716 724 ### I18N ################################################################## 717 725 … … 894 902 895 903 # Allow first argument to be a variable. 896 'cache08': ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'), 897 'cache09': ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'), 898 'cache10': ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'), 904 'cache08': ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'), 905 'cache09': ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'), 906 'cache10': ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'), 899 907 900 908 # Raise exception if we don't have at least 2 args, first one integer. … … 902 910 'cache12': ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 903 911 'cache13': ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 904 'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), 905 'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError), 912 'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), 913 'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError), 906 914 907 915 ### AUTOESCAPE TAG ##############################################
