Django

Code

Changeset 8095

Show
Ignore:
Timestamp:
07/26/08 17:09:43 (4 months ago)
Author:
mtredinnick
Message:

Fixed #4534 -- Added an "else" option to the "ifchanged" template tag.
Patch from SmileyChris?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaulttags.py

    r7758 r8095  
    158158 
    159159class IfChangedNode(Node): 
    160     def __init__(self, nodelist, *varlist): 
    161         self.nodelist = nodelist 
     160    def __init__(self, nodelist_true, nodelist_false, *varlist): 
     161        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 
    162162        self._last_seen = None 
    163163        self._varlist = map(Variable, varlist) 
     
    174174                compare_to = [var.resolve(context) for var in self._varlist] 
    175175            else: 
    176                 compare_to = self.nodelist.render(context) 
     176                compare_to = self.nodelist_true.render(context) 
    177177        except VariableDoesNotExist: 
    178178            compare_to = None 
    179179 
    180         if compare_to != self._last_seen: 
     180        if compare_to != self._last_seen: 
    181181            firstloop = (self._last_seen == None) 
    182182            self._last_seen = compare_to 
    183183            context.push() 
    184184            context['ifchanged'] = {'firstloop': firstloop} 
    185             content = self.nodelist.render(context) 
     185            content = self.nodelist_true.render(context) 
    186186            context.pop() 
    187187            return content 
    188         else: 
    189             return '' 
     188        elif self.nodelist_false: 
     189            return self.nodelist_false.render(context) 
     190        return '' 
    190191 
    191192class IfEqualNode(Node): 
     
    804805    """ 
    805806    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:]) 
    809815ifchanged = register.tag(ifchanged) 
    810816 
  • django/trunk/docs/templates.txt

    r8081 r8095  
    828828            {% endifchanged %} 
    829829        {% endfor %} 
     830 
     831The ``ifchanged`` tag also takes an optional ``{% else %}`` clause that will 
     832be 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 %} 
    830843 
    831844ifequal 
  • django/trunk/tests/regressiontests/templates/tests.py

    r7754 r8095  
    566566            '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'), 
    567567 
     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 
    568576            ### IFEQUAL TAG ########################################################### 
    569577            'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""), 
     
    713721                # Inheritance from a template that doesn't have any blocks 
    714722                'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'), 
    715                  
     723 
    716724            ### I18N ################################################################## 
    717725 
     
    894902 
    895903            # 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'), 
    899907 
    900908            # Raise exception if we don't have at least 2 args, first one integer. 
     
    902910            'cache12': ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 
    903911            '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), 
    906914 
    907915            ### AUTOESCAPE TAG ##############################################