Code

Opened 3 years ago

Closed 16 months ago

#16355 closed Bug (wontfix)

TemplateSyntaxError raised by do_block do not allow using doublicate block

Reported by: netinsideout@… Owned by: anonymous
Component: Template system Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This ticket is related to changeset: https://code.djangoproject.com/changeset/12655 and ticket:
https://code.djangoproject.com/ticket/10975
I realised that django template system supports construction like this:

{% ifnotequal setting u'inverted'%}
   {% block a %}{% endblock %}
   {% block b %}{% endblock %}
{% else %}
   {% block b %}{% endblock %}
   {% block a %}{% endblock %}
{% endifnotequal %}

but function do_block raises TemplateSyntaxError when checking for duplicate block in template.

Original in django 1.3:
django/template/loader_tags.py

def do_block(parser, token):
    """
    Define a block that can be overridden by child templates.
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError("'%s' tag takes only one argument" % bits[0])
    block_name = bits[1]
    # Keep track of the names of BlockNodes found in this template, so we can
    # check for duplication.
    try:
        if block_name in parser.__loaded_blocks:
            raise TemplateSyntaxError("'%s' tag with name '%s' appears more than once" % (bits[0], block_name))
        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', 'endblock %s' % block_name))
    parser.delete_first_token()
    return BlockNode(block_name, nodelist)

I just removed 2 lines and construction like above starts working as expected.

def do_block(parser, token):
    """
    Define a block that can be overridden by child templates.
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError("'%s' tag takes only one argument" % bits[0])
    block_name = bits[1]
    # Keep track of the names of BlockNodes found in this template, so we can
    # check for duplication.
    try:
        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', 'endblock %s' % block_name))
    parser.delete_first_token()
    return BlockNode(block_name, nodelist)

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by manfre

  • Has patch unset
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to anonymous
  • Patch needs improvement unset
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Design decision needed

Setting as DDN because this should be brought to the developers list.

Quoting ubernostrum from #10975:

"A block cannot be defined conditionally within any sort of "if" construct, and this is by design; the structure of the template is the structure of the template."

If you disagree, please take it to the django-developers list.

Didn't mark as wontfix because in same ticket SmileyChris stated that wontfix bug was fixed.

comment:2 Changed 3 years ago by jacob

  • milestone 1.4 deleted

Milestone 1.4 deleted

comment:3 Changed 16 months ago by aaugustin

  • Resolution set to wontfix
  • Status changed from assigned to closed

SmileyChris refers to a more specific inconsistency between if and ifequal: https://code.djangoproject.com/ticket/6510#comment:16

I believe ubernostrum's comment also applies here.

(Taking this to django-developers is also the way to go if you think my wontfix is incorrect.)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.