Ticket #2800: enhanced-ifchanged.patch
File enhanced-ifchanged.patch, 2.5 KB (added by , 18 years ago) |
---|
-
django/template/defaulttags.py
120 120 return nodelist.render(context) 121 121 122 122 class IfChangedNode(Node): 123 def __init__(self, nodelist ):123 def __init__(self, nodelist, var1=None): 124 124 self.nodelist = nodelist 125 125 self._last_seen = None 126 self._var1 = var1 126 127 127 128 def render(self, context): 128 content = self.nodelist.render(context) 129 if content != self._last_seen: 129 try: 130 if self._var1==None: 131 compare_to = self.nodelist.render(context) 132 else: 133 compare_to = resolve_variable(self._var1, context) 134 except VariableDoesNotExist: 135 compare_to = None 136 if compare_to != self._last_seen: 130 137 firstloop = (self._last_seen == None) 131 self._last_seen = co ntent138 self._last_seen = compare_to 132 139 context.push() 133 140 context['ifchanged'] = {'firstloop': firstloop} 134 141 content = self.nodelist.render(context) … … 626 633 """ 627 634 Check if a value has changed from the last iteration of a loop. 628 635 629 The 'ifchanged' block tag is used within a loop. It checks its own rendered 630 contents against its previous state and only displays its content if the 631 value has changed:: 636 The 'ifchanged' block tag is used within a loop. It has two possible uses. 637 1) It checks its own rendered contents against its previous state and 638 only displays its content if the value has changed. 639 2) It checks if the given variable has changed. :: 632 640 633 641 <h1>Archive for {{ year }}</h1> 634 642 635 643 {% for date in days %} 636 644 {% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %} 645 {% ifchanged date %}date has changed{% endifchanged %} 637 646 <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a> 638 647 {% endfor %} 639 648 """ 640 649 bits = token.contents.split() 641 if len(bits) != 1: 642 raise TemplateSyntaxError, "'ifchanged' tag takes no arguments" 650 if len(bits) > 2: 651 raise TemplateSyntaxError, "'ifchanged' tag takes either one or none argument" 652 if len(bits) == 1: 653 bits.append(None) 643 654 nodelist = parser.parse(('endifchanged',)) 644 655 parser.delete_first_token() 645 return IfChangedNode(nodelist )656 return IfChangedNode(nodelist, bits[1]) 646 657 ifchanged = register.tag(ifchanged) 647 658 648 659 #@register.tag