mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17187 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -250,31 +250,37 @@ class IfEqualNode(Node):
|
||||
return self.nodelist_false.render(context)
|
||||
|
||||
class IfNode(Node):
|
||||
child_nodelists = ('nodelist_true', 'nodelist_false')
|
||||
|
||||
def __init__(self, var, nodelist_true, nodelist_false=None):
|
||||
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
|
||||
self.var = var
|
||||
def __init__(self, conditions_nodelists):
|
||||
self.conditions_nodelists = conditions_nodelists
|
||||
|
||||
def __repr__(self):
|
||||
return "<If node>"
|
||||
return "<IfNode>"
|
||||
|
||||
def __iter__(self):
|
||||
for node in self.nodelist_true:
|
||||
yield node
|
||||
for node in self.nodelist_false:
|
||||
yield node
|
||||
for _, nodelist in self.conditions_nodelists:
|
||||
for node in nodelist:
|
||||
yield node
|
||||
|
||||
@property
|
||||
def nodelist(self):
|
||||
return NodeList(node for _, nodelist in self.conditions_nodelists for node in nodelist)
|
||||
|
||||
def render(self, context):
|
||||
try:
|
||||
var = self.var.eval(context)
|
||||
except VariableDoesNotExist:
|
||||
var = None
|
||||
for condition, nodelist in self.conditions_nodelists:
|
||||
|
||||
if var:
|
||||
return self.nodelist_true.render(context)
|
||||
else:
|
||||
return self.nodelist_false.render(context)
|
||||
if condition is not None: # if / elif clause
|
||||
try:
|
||||
match = condition.eval(context)
|
||||
except VariableDoesNotExist:
|
||||
match = None
|
||||
else: # else clause
|
||||
match = True
|
||||
|
||||
if match:
|
||||
return nodelist.render(context)
|
||||
|
||||
return ''
|
||||
|
||||
class RegroupNode(Node):
|
||||
def __init__(self, target, expression, var_name):
|
||||
@@ -825,6 +831,8 @@ def do_if(parser, token):
|
||||
|
||||
{% if athlete_list %}
|
||||
Number of athletes: {{ athlete_list|count }}
|
||||
{% elif athlete_in_locker_room_list %}
|
||||
Athletes should be out of the locker room soon!
|
||||
{% else %}
|
||||
No athletes.
|
||||
{% endif %}
|
||||
@@ -832,8 +840,9 @@ def do_if(parser, token):
|
||||
In the above, if ``athlete_list`` is not empty, the number of athletes will
|
||||
be displayed by the ``{{ athlete_list|count }}`` variable.
|
||||
|
||||
As you can see, the ``if`` tag can take an option ``{% else %}`` clause
|
||||
that will be displayed if the test fails.
|
||||
As you can see, the ``if`` tag may take one or several `` {% elif %}``
|
||||
clauses, as well as an ``{% else %}`` clause that will be displayed if all
|
||||
previous conditions fail. These clauses are optional.
|
||||
|
||||
``if`` tags may use ``or``, ``and`` or ``not`` to test a number of
|
||||
variables or to negate a given variable::
|
||||
@@ -871,16 +880,32 @@ def do_if(parser, token):
|
||||
|
||||
Operator precedence follows Python.
|
||||
"""
|
||||
# {% if ... %}
|
||||
bits = token.split_contents()[1:]
|
||||
var = TemplateIfParser(parser, bits).parse()
|
||||
nodelist_true = parser.parse(('else', 'endif'))
|
||||
condition = TemplateIfParser(parser, bits).parse()
|
||||
nodelist = parser.parse(('elif', 'else', 'endif'))
|
||||
conditions_nodelists = [(condition, nodelist)]
|
||||
token = parser.next_token()
|
||||
|
||||
# {% elif ... %} (repeatable)
|
||||
while token.contents.startswith('elif'):
|
||||
bits = token.split_contents()[1:]
|
||||
condition = TemplateIfParser(parser, bits).parse()
|
||||
nodelist = parser.parse(('elif', 'else', 'endif'))
|
||||
conditions_nodelists.append((condition, nodelist))
|
||||
token = parser.next_token()
|
||||
|
||||
# {% else %} (optional)
|
||||
if token.contents == 'else':
|
||||
nodelist_false = parser.parse(('endif',))
|
||||
parser.delete_first_token()
|
||||
else:
|
||||
nodelist_false = NodeList()
|
||||
return IfNode(var, nodelist_true, nodelist_false)
|
||||
nodelist = parser.parse(('endif',))
|
||||
conditions_nodelists.append((None, nodelist))
|
||||
token = parser.next_token()
|
||||
|
||||
# {% endif %}
|
||||
assert token.contents == 'endif'
|
||||
|
||||
return IfNode(conditions_nodelists)
|
||||
|
||||
|
||||
@register.tag
|
||||
def ifchanged(parser, token):
|
||||
|
||||
Reference in New Issue
Block a user