nested block definitions

32 views
Skip to first unread message

Michael Feingold

unread,
Sep 21, 2009, 11:25:38 AM9/21/09
to Django users
I am somewhat confused about the semantics for nested block
definitions.Let me explain:
{% block %} tag serves two purposes a) define a hole (along with the
default value) to be filled later and b) define the content to replace
the current value of the hole.

As long as we are talking about a) I perfectly understand and have no
problems with the block tag being nested inside any other tag -
including for, if, and block itself - anything. If a new value is
supplied for the block it just placed there instead of whatever is
already there.

Now with b) I am lost. For example, what is the meaning of this:

=== Base Template ===
...
{% block a %}
...
{% endblock %}
...

=== Derived Template ===
{%extends "Base Template" %)
{% for i in list %}
{% block a %}
current value={{i}}
{% endblock %}
{% endfor %}

=====

What value will be placed in the result? is this even a valid
construction?

It seems to me that the block tags defining the replacement values
should only be allowed on the top level of the template definition. In
other words the derived template from the above example should not be
considered a valid template.
Am I missing something here?

Daniel Roseman

unread,
Sep 21, 2009, 11:37:10 AM9/21/09
to Django users
No, you are correct. Anything in a child template that is outside of a
{% block %} is ignored. Your derived template is not valid - it may
not actually raise an error, but its behaviour is undefined.
--
DR.

Karen Tracey

unread,
Sep 21, 2009, 11:48:07 AM9/21/09
to django...@googlegroups.com

What happened when you tried it?  What I'd expect would be that you'd get an "a" block placed in the proper spot in the parent template that contains:

current value=

The {% for %} and {% endfor %} appear outside of a {% block %} tag in the child template, so they are for all intents and purposes not there.  Well, they are there (you'll get a template syntax error, for example, if you leave off the {% endfor %}) but they have no place to go in the parent template.  Only the text contained in the {% block a %} from the child template is placed in the parent template, and that text does not contain anything that assigns a value to i, so unless i has a value from some other assignment (for example if it is in the base context for rendering), there will be no value found to render for {{i}} from the child template.


is this even a valid
construction?


It won't be reported as an error but anything outside of a {% block %} tag in a child template is usually a mistake.  I don't know enough about the template engine to comment on why such stuff isn't reported as an error.  There may be a good reason for it, I don't know.

 Karen

Michael Feingold

unread,
Sep 21, 2009, 1:27:13 PM9/21/09
to Django users
> No, you are correct. Anything in a child template that is outside of a
> {% block %} is ignored....

What about inside a {% block %}? Is it allowed to define an "override"
inside another block. In other words would wrapping the offending
construct in another block like in:

=== Derived Template ===
{%extends "Base Template" %)
<%block newblock%}
{% for i in list %}
{% block a %}
current value={{i}}
{% endblock %}
{% endfor %}
{% endblock %}
==========
make it valid? I guess not, am I right?

To summarize, the {% extends %} can only consist of overriding blocks
- no new ones, and inside those, nested blocks are only allowed to
define new blocks - no override definitions. Would you agree?
> DR.- Hide quoted text -
>
> - Show quoted text -

Brian Neal

unread,
Sep 21, 2009, 5:07:18 PM9/21/09
to Django users
On Sep 21, 10:37 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
>
> Anything in a child template that is outside of a
> {% block %} is ignored.
>

I usually have a few {% load %} tags in child templates above {% block
%} to bring in needed template tags.

BN
Reply all
Reply to author
Forward
0 new messages