From a0c354ee4ed02bb3f958dfb029d29b76e6637952 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 12 Feb 2007 00:22:22 +0000 Subject: [PATCH] Fixed #3351 -- Added optional naming of the block in "endblock" tags to ensure correct nesting. Thanks for the patch, SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4489 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/loader_tags.py | 2 +- docs/templates.txt | 5 +++++ tests/regressiontests/templates/tests.py | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py index e329b1bb36f..0e11b12c215 100644 --- a/django/template/loader_tags.py +++ b/django/template/loader_tags.py @@ -129,7 +129,7 @@ def do_block(parser, token): parser.__loaded_blocks.append(block_name) except AttributeError: # parser.__loaded_blocks isn't a list yet parser.__loaded_blocks = [block_name] - nodelist = parser.parse(('endblock',)) + nodelist = parser.parse(('endblock','endblock %s' % block_name)) parser.delete_first_token() return BlockNode(block_name, nodelist) diff --git a/docs/templates.txt b/docs/templates.txt index a3d60c54008..9b3825f9f71 100644 --- a/docs/templates.txt +++ b/docs/templates.txt @@ -253,6 +253,11 @@ Here are some tips for working with inheritance: if you want to add to the contents of a parent block instead of completely overriding it. + * You can optionally name your ``{{ endblock }}`` tag with the same name + you gave the ``{{ block }}`` tag (for example, ``{{ endblock content }}``). + In larger templates this helps you see which ``{{ block }}`` tags are + being closed. + Finally, note that you can't define multiple ``{% block %}`` tags with the same name in the same template. This limitation exists because a block tag works in "both" directions. That is, a block tag doesn't just provide a hole to fill -- diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 0a41f5b5b7b..620825b254e 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -390,6 +390,21 @@ class Templates(unittest.TestCase): 'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"), 'include04': ('a{% include "nonexistent" %}b', {}, "ab"), + ### NAMED ENDBLOCKS ####################################################### + + # Basic test + 'namedendblocks01': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock first %}3", {}, '1_2_3'), + + # Unbalanced blocks + 'namedendblocks02': ("1{% block first %}_{% block second %}2{% endblock first %}_{% endblock %}3", {}, template.TemplateSyntaxError), + 'namedendblocks03': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock second %}3", {}, template.TemplateSyntaxError), + 'namedendblocks04': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock third %}3", {}, template.TemplateSyntaxError), + 'namedendblocks05': ("1{% block first %}_{% block second %}2{% endblock first %}", {}, template.TemplateSyntaxError), + + # Mixed named and unnamed endblocks + 'namedendblocks06': ("1{% block first %}_{% block second %}2{% endblock %}_{% endblock first %}3", {}, '1_2_3'), + 'namedendblocks07': ("1{% block first %}_{% block second %}2{% endblock second %}_{% endblock %}3", {}, '1_2_3'), + ### INHERITANCE ########################################################### # Standard template with no inheritance