Code

Ticket #3351: endblock.patch

File endblock.patch, 3.1 KB (added by SmileyChris, 7 years ago)
  • django/template/loader_tags.py

     
    129129        parser.__loaded_blocks.append(block_name) 
    130130    except AttributeError: # parser.__loaded_blocks isn't a list yet 
    131131        parser.__loaded_blocks = [block_name] 
    132     nodelist = parser.parse(('endblock',)) 
     132    try: 
     133        nodelist = parser.parse(('endblock','endblock %s' % block_name)) 
     134    except TemplateSyntaxError, err: 
     135        raise TemplateSyntaxError, str(err) + ' (unbalanced %s tags?)' % block_name 
    133136    parser.delete_first_token() 
    134137    return BlockNode(block_name, nodelist) 
    135138 
  • docs/templates.txt

     
    253253      if you want to add to the contents of a parent block instead of 
    254254      completely overriding it. 
    255255 
     256    * You can optionally name your ``{{ endblock }}`` tag with the same name 
     257      you gave the ``{{ block }}`` tag (for example, ``{{ endblock content }}``). 
     258      In larger templates this helps you see which ``{{ block }}`` tags are 
     259      being closed. 
     260 
    256261Finally, note that you can't define multiple ``{% block %}`` tags with the same 
    257262name in the same template. This limitation exists because a block tag works in 
    258263"both" directions. That is, a block tag doesn't just provide a hole to fill -- 
  • tests/regressiontests/templates/tests.py

     
    390390            'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"), 
    391391            'include04': ('a{% include "nonexistent" %}b', {}, "ab"), 
    392392 
     393            ### NAMED ENDBLOCKS ####################################################### 
     394 
     395            # Basic test 
     396            'namedendblocks01': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock first %}3", {}, '1_2_3'), 
     397 
     398            # Unbalanced blocks 
     399            'namedendblocks02': ("1{% block first %}_{% block second %}2{% endblock first %}_{% endblock %}3", {}, template.TemplateSyntaxError), 
     400            'namedendblocks03': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock second %}3", {}, template.TemplateSyntaxError), 
     401            'namedendblocks04': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock third %}3", {}, template.TemplateSyntaxError), 
     402 
     403            # Mixed named and unnamed endblocks 
     404            'namedendblocks05': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock first %}3", {}, '1_2_3'), 
     405            'namedendblocks06': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock %}3", {}, '1_2_3'), 
     406 
    393407            ### INHERITANCE ########################################################### 
    394408 
    395409            # Standard template with no inheritance