Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#13413 closed (invalid)

blocks ignore if condition

Reported by: lanyjie Owned by: nobody
Component: Template system Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Here is a quick recapture:

base.html:
{% block A %}  {% endblock %}
{% block B %}  {% endblock %}

cond.html:
{% extends "base.html" %}
{% if error %}
{% block A %} ERROR! {% endblock %}
{% else %}
{% block B %} NO ERROR! {% endblock %}
{% endif %}

No matter if there is error or not, you will always get:

ERROR! 
NO ERROR!

Attachments (0)

Change History (4)

comment:1 Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Child templates can't have logic (outside of {% block %}s). They simply provide blocks that can override the content in the extended template. Anything outside of a {% block %} in a child template is effectively invisible to the template engine. The base template here defines blocks A and B, the child template overrides both, so both blocks get the content from the child.

comment:2 Changed 4 years ago by russellm

I would also add that this is exactly what I told you when I closed #13399. This behavior is *by design* - it isn't a bug.

comment:3 Changed 4 years ago by lanyjie

Thanks for the clearance. Such a design makes life easier (especially for implementing the engine), although somehow limits the power of the template system. It would be nice if the compiler gives some warnings or even errors when you have logic outside a block. And it would be good to explicitly stipulate that in the documentation, that blocks must be top level units in the child template, and most things outside a block is invisible to the engine and the base template. Probably, we can explicit stipulate that only {% extends %}, {% load %}, {% comment %}, maybe a few others are allowed outside blocks in the child template. If that's the case, then here is another possible issue:

base.html:
{% block a %} {% endblock %}

child.html:
some text. {% block a %} block a. {% endblock %}

the engine would produce this:

some text. block a.

But all expected is just "block a." Would that be a mild bug easy to fix? I noticed a similar ticket #7324, where you have nested blocks, and by the design, that is also invalid, and it might be helpful if the documentation says that nested blocks are no good (as an example of violating the designed rules).

comment:4 Changed 4 years ago by SmileyChris

Now you're just making stuff up:

In [1]: from django.template import *

In [2]: a = Template('{% block a %}{% endblock %}')

In [3]: Template('{% extends a %}some text. {% block a %}block a.{% endblock %}').render(Context({'a': a}))
Out[3]: u'block a.'

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.