Ticket #4534: ifchanged-else.patch

File ifchanged-else.patch, 4.6 KB (added by Chris Beaven, 17 years ago)

with tests and docs

  • django/template/defaulttags.py

     
    127127        return nodelist.render(context)
    128128
    129129class IfChangedNode(Node):
    130     def __init__(self, nodelist, *varlist):
    131         self.nodelist = nodelist
     130    def __init__(self, nodelist_true, nodelist_false, *varlist):
     131        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    132132        self._last_seen = None
    133133        self._varlist = varlist
    134134
     
    141141                # This automatically behaves like a OR evaluation of the multiple variables.
    142142                compare_to = [resolve_variable(var, context) for var in self._varlist]
    143143            else:
    144                 compare_to = self.nodelist.render(context)
     144                compare_to = self.nodelist_true.render(context)
    145145        except VariableDoesNotExist:
    146146            compare_to = None
    147147
     
    150150            self._last_seen = compare_to
    151151            context.push()
    152152            context['ifchanged'] = {'firstloop': firstloop}
    153             content = self.nodelist.render(context)
     153            content = self.nodelist_true.render(context)
    154154            context.pop()
    155155            return content
    156156        else:
    157             return ''
     157            if self.nodelist_false:
     158                return self.nodelist_false.render(context)
     159            else:
     160                return ''
    158161
    159162class IfEqualNode(Node):
    160163    def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
     
    718721            {% endfor %}
    719722    """
    720723    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:])
    724732ifchanged = register.tag(ifchanged)
    725733
    726734#@register.tag
  • docs/templates.txt

     
    566566            {% endifchanged %}
    567567        {% endfor %}
    568568
     569The ``ifchanged`` tag can also take an optional ``{% else %}`` clause that
     570will 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
    569582ifequal
    570583~~~~~~~
    571584
  • tests/regressiontests/templates/tests.py

     
    391391            # ifchanged for the day.
    392392            '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'),
    393393
     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
    394402            ### IFEQUAL TAG ###########################################################
    395403            'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""),
    396404            'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
Back to Top