Ticket #3100: django_elif.diff
File django_elif.diff, 8.8 KB (added by , 15 years ago) |
---|
-
django/template/__init__.py
diff --git a/django/template/__init__.py b/django/template/__init__.py index 4c386be..881e16a 100644
a b class Parser(object): 275 275 var_node = self.create_variable_node(filter_expression) 276 276 self.extend_nodelist(nodelist, var_node,token) 277 277 elif token.token_type == TOKEN_BLOCK: 278 if token.contents in parse_until:279 # put token back on token list so calling code knows why it terminated280 self.prepend_token(token)281 return nodelist282 278 try: 283 279 command = token.contents.split()[0] 284 280 except IndexError: 285 281 self.empty_block_tag(token) 282 if command in parse_until: 283 # put token back on token list so calling code knows why it terminated 284 self.prepend_token(token) 285 return nodelist 286 286 # execute callback function for this tag and append resulting node 287 287 self.enter_command(command, token) 288 288 try: -
django/template/defaulttags.py
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 2ccfc6a..28b3385 100644
a b class IfEqualNode(Node): 231 231 return self.nodelist_false.render(context) 232 232 233 233 class IfNode(Node): 234 def __init__(self, var, nodelist_true, nodelist_false=None):235 self. nodelist_true, self.nodelist_false = nodelist_true, nodelist_false236 self. var = var234 def __init__(self, condition_nodelists, else_nodelist): 235 self.condition_nodelists = condition_nodelists 236 self.else_nodelist = else_nodelist 237 237 238 238 def __repr__(self): 239 239 return "<If node>" 240 240 241 241 def __iter__(self): 242 for node in self.nodelist_true: 243 yield node 244 for node in self.nodelist_false: 242 for condition, nodelist in self.condition_nodelists: 243 for node in nodelist: 244 yield node 245 246 for node in else_nodelist: 245 247 yield node 246 248 247 249 def get_nodes_by_type(self, nodetype): 248 250 nodes = [] 249 251 if isinstance(self, nodetype): 250 252 nodes.append(self) 251 nodes.extend(self.nodelist_true.get_nodes_by_type(nodetype)) 252 nodes.extend(self.nodelist_false.get_nodes_by_type(nodetype)) 253 254 for condition, nodelist in self.condition_nodelists: 255 nodes.extend(nodelist.get_nodes_by_type(nodetype)) 256 257 nodes.extend(self.else_nodelist.get_nodes_by_type(nodetype)) 258 253 259 return nodes 254 260 255 261 def render(self, context): 256 if self.var.eval(context): 257 return self.nodelist_true.render(context) 258 else: 259 return self.nodelist_false.render(context) 262 for condition, nodelist in self.condition_nodelists: 263 if condition.eval(context): 264 return nodelist.render(context) 265 266 return self.else_nodelist.render(context) 260 267 261 268 class RegroupNode(Node): 262 269 def __init__(self, target, expression, var_name): … … def do_if(parser, token): 788 795 As you can see, the ``if`` tag can take an option ``{% else %}`` clause 789 796 that will be displayed if the test fails. 790 797 798 If you have multiple conditions to check against, you may wish to use 799 the ``elif`` tag:: 800 801 {% if athlete_list %} 802 Number of athletes: {{ athlete_list|length }} 803 {% elif athletes_in_lockerroom %} 804 There are some athletes queued in the locker room, but they 805 should be out soon! 806 {% else %} 807 No athletes. 808 {% endif %} 809 791 810 ``if`` tags may use ``or``, ``and`` or ``not`` to test a number of 792 811 variables or to negate a given variable:: 793 812 … … def do_if(parser, token): 824 843 825 844 Operator precedence follows Python. 826 845 """ 827 bits = token.split_contents()[1:] 828 var = TemplateIfParser(parser, bits).parse() 829 nodelist_true = parser.parse(('else', 'endif')) 830 token = parser.next_token() 831 if token.contents == 'else': 832 nodelist_false = parser.parse(('endif',)) 833 parser.delete_first_token() 834 else: 835 nodelist_false = NodeList() 836 return IfNode(var, nodelist_true, nodelist_false) 846 # The condition nodelist is constructed of: 847 # [(<if_var>, <nodelist>), 848 # <if_var>, <nodelist>])] 849 # 850 # So as soon as one of these conditions evaluates to True, the 851 # accompanying nodelist is evaluated. 852 condition_nodelists = [] 853 else_nodelist = None 854 all_parsed = False 855 856 while not all_parsed: 857 bits = token.split_contents()[1:] 858 var = TemplateIfParser(parser, bits).parse() 859 nodelist = parser.parse(('elif', 'else', 'endif')) 860 condition_nodelists.append((var, nodelist)) 861 862 token = parser.next_token() 863 command = token.contents.split()[0] 864 if command == 'elif': 865 pass 866 elif command == 'else': 867 else_nodelist = parser.parse(('endif',)) 868 parser.delete_first_token() 869 all_parsed = True 870 elif command == 'endif': 871 all_parsed = True 872 873 if not else_nodelist: 874 else_nodelist = NodeList() 875 876 return IfNode(condition_nodelists, else_nodelist) 837 877 do_if = register.tag("if", do_if) 838 878 839 879 #@register.tag -
docs/ref/templates/builtins.txt
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 51ef03c..16f1820 100644
a b displayed by the ``{{ athlete_list|length }}`` variable. 313 313 As you can see, the ``if`` tag can take an optional ``{% else %}`` clause that 314 314 will be displayed if the test fails. 315 315 316 .. versionadded:: 1.2 317 318 If you have multiple conditions to check against, you may wish to use 319 the ``elif`` tag:: 320 321 {% if athlete_list %} 322 Number of athletes: {{ athlete_list|length }} 323 {% elif athletes_in_lockerroom %} 324 There are some athletes queued in the locker room, but they 325 should be out soon! 326 {% else %} 327 No athletes. 328 {% endif %} 329 316 330 Boolean operators 317 331 ^^^^^^^^^^^^^^^^^ 318 332 -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 2946208..99aacff 100644
a b class Templates(unittest.TestCase): 542 542 'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"), 543 543 'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"), 544 544 545 'if-tag04': ("{% if foo %}foo{% elif bar %}bar{% else %}nothing{%endif%}", 546 {'foo': True}, "foo"), 547 'if-tag05': ("{% if foo %}foo{% elif bar %}bar{% else %}nothing{%endif%}", 548 {'bar': True}, "bar"), 549 'if-tag06': ("{% if foo %}foo{% elif bar %}bar{% else %}nothing{%endif%}", 550 {}, "nothing"), 551 545 552 # Filters 546 553 'if-tag-filter01': ("{% if foo|length == 5 %}yes{% else %}no{% endif %}", {'foo': 'abcde'}, "yes"), 547 554 'if-tag-filter02': ("{% if foo|upper == 'ABC' %}yes{% else %}no{% endif %}", {}, "no"), … … class Templates(unittest.TestCase): 741 748 'namedendblocks01': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock first %}3", {}, '1_2_3'), 742 749 743 750 # Unbalanced blocks 744 'namedendblocks02': ("1{% block first %}_{% block second %}2{% endblock first %}_{% endblock second %}3", {}, template.TemplateSyntaxError), 745 'namedendblocks03': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock second %}3", {}, template.TemplateSyntaxError), 746 'namedendblocks04': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock third %}3", {}, template.TemplateSyntaxError), 747 'namedendblocks05': ("1{% block first %}_{% block second %}2{% endblock first %}", {}, template.TemplateSyntaxError), 751 'namedendblocks02': ("1{% block first %}_{% block second %}2{% endblock first %}", {}, template.TemplateSyntaxError), 748 752 749 753 # Mixed named and unnamed endblocks 750 'namedendblocks0 6': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock first %}3", {}, '1_2_3'),751 'namedendblocks0 7': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock %}3", {}, '1_2_3'),754 'namedendblocks03': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock first %}3", {}, '1_2_3'), 755 'namedendblocks04': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock %}3", {}, '1_2_3'), 752 756 753 757 ### INHERITANCE ########################################################### 754 758