Opened 15 years ago

Closed 15 years ago

Last modified 15 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: no UI/UX: no

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!

Change History (4)

comment:1 by Karen Tracey, 15 years ago

Resolution: invalid
Status: newclosed

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 by Russell Keith-Magee, 15 years ago

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 by lanyjie, 15 years ago

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 by Chris Beaven, 15 years ago

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.'
Note: See TracTickets for help on using tickets.
Back to Top