Fixed #4534 -- Added an "else" option to the "ifchanged" template tag.
Patch from SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8095 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f763ff4d02
commit
e29aece743
|
@ -157,8 +157,8 @@ class ForNode(Node):
|
||||||
return nodelist.render(context)
|
return nodelist.render(context)
|
||||||
|
|
||||||
class IfChangedNode(Node):
|
class IfChangedNode(Node):
|
||||||
def __init__(self, nodelist, *varlist):
|
def __init__(self, nodelist_true, nodelist_false, *varlist):
|
||||||
self.nodelist = nodelist
|
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
|
||||||
self._last_seen = None
|
self._last_seen = None
|
||||||
self._varlist = map(Variable, varlist)
|
self._varlist = map(Variable, varlist)
|
||||||
self._id = str(id(self))
|
self._id = str(id(self))
|
||||||
|
@ -173,20 +173,21 @@ class IfChangedNode(Node):
|
||||||
# like an OR evaluation of the multiple variables.
|
# like an OR evaluation of the multiple variables.
|
||||||
compare_to = [var.resolve(context) for var in self._varlist]
|
compare_to = [var.resolve(context) for var in self._varlist]
|
||||||
else:
|
else:
|
||||||
compare_to = self.nodelist.render(context)
|
compare_to = self.nodelist_true.render(context)
|
||||||
except VariableDoesNotExist:
|
except VariableDoesNotExist:
|
||||||
compare_to = None
|
compare_to = None
|
||||||
|
|
||||||
if compare_to != self._last_seen:
|
if compare_to != self._last_seen:
|
||||||
firstloop = (self._last_seen == None)
|
firstloop = (self._last_seen == None)
|
||||||
self._last_seen = compare_to
|
self._last_seen = compare_to
|
||||||
context.push()
|
context.push()
|
||||||
context['ifchanged'] = {'firstloop': firstloop}
|
context['ifchanged'] = {'firstloop': firstloop}
|
||||||
content = self.nodelist.render(context)
|
content = self.nodelist_true.render(context)
|
||||||
context.pop()
|
context.pop()
|
||||||
return content
|
return content
|
||||||
else:
|
elif self.nodelist_false:
|
||||||
return ''
|
return self.nodelist_false.render(context)
|
||||||
|
return ''
|
||||||
|
|
||||||
class IfEqualNode(Node):
|
class IfEqualNode(Node):
|
||||||
def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
|
def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
|
||||||
|
@ -803,9 +804,14 @@ def ifchanged(parser, token):
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
"""
|
"""
|
||||||
bits = token.contents.split()
|
bits = token.contents.split()
|
||||||
nodelist = parser.parse(('endifchanged',))
|
nodelist_true = parser.parse(('else', 'endifchanged'))
|
||||||
parser.delete_first_token()
|
token = parser.next_token()
|
||||||
return IfChangedNode(nodelist, *bits[1:])
|
if token.contents == 'else':
|
||||||
|
nodelist_false = parser.parse(('endifchanged',))
|
||||||
|
parser.delete_first_token()
|
||||||
|
else:
|
||||||
|
nodelist_false = NodeList()
|
||||||
|
return IfChangedNode(nodelist_true, nodelist_false, *bits[1:])
|
||||||
ifchanged = register.tag(ifchanged)
|
ifchanged = register.tag(ifchanged)
|
||||||
|
|
||||||
#@register.tag
|
#@register.tag
|
||||||
|
|
|
@ -828,6 +828,19 @@ The 'ifchanged' block tag is used within a loop. It has two possible uses.
|
||||||
{% endifchanged %}
|
{% endifchanged %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
The ``ifchanged`` tag also takes an optional ``{% else %}`` clause that will
|
||||||
|
be displayed if the value has not changed::
|
||||||
|
|
||||||
|
{% for match in matches %}
|
||||||
|
<div style="background-color:
|
||||||
|
{% ifchanged match.ballot_id %}
|
||||||
|
{% cycle red,blue %}
|
||||||
|
{% else %}
|
||||||
|
grey
|
||||||
|
{% endifchanged %}
|
||||||
|
">{{ match }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
ifequal
|
ifequal
|
||||||
~~~~~~~
|
~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -565,6 +565,14 @@ class Templates(unittest.TestCase):
|
||||||
# ifchanged for the day.
|
# ifchanged for the day.
|
||||||
'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'),
|
'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'),
|
||||||
|
|
||||||
|
# Test the else clause of ifchanged.
|
||||||
|
'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,'),
|
||||||
|
|
||||||
|
'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,'),
|
||||||
|
'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,'),
|
||||||
|
|
||||||
|
'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'),
|
||||||
|
|
||||||
### IFEQUAL TAG ###########################################################
|
### IFEQUAL TAG ###########################################################
|
||||||
'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""),
|
'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""),
|
||||||
'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
|
'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
|
||||||
|
|
Loading…
Reference in New Issue