Template inheritance and {% include %} tags

92 views
Skip to first unread message

Michael Elsdörfer

unread,
Sep 18, 2008, 1:39:05 PM9/18/08
to Django developers
I remember this coming up on django-users and IRC once or twice, and
never thought too much about it, but currently, template inheritance
and includes don't work together at all:

* Blocks included in a parent template cannot be overwritten in a
child template.
* Blocks from an include in a child template cannot overwrite blocks
in a parent template.

Now that I was in a situation myself were this would have been handy,
I wonder why that doesn't (shouldn't?) work: It turns out that
changing ConstantIncudeNode to expose ``make self.template.nodelist``
as ``self.nodelist`` is all that is necessary (tests still pass).
Granted, for dynamic includes it is probably harder or possibly even
undoable (I haven't looked into that), but why not enable it for the
presumable prevalent use case (static includes)?

Am I overlooking something, or was there just no demand so far?

Related tickets: #7321 (invalid), possibly: #6646 (design decision
needed).

Michael

Simon Willison

unread,
Sep 18, 2008, 5:17:28 PM9/18/08
to Django developers
On Sep 18, 6:39 pm, Michael Elsdörfer <elsdoer...@gmail.com> wrote:
> I remember this coming up on django-users and IRC once or twice, and
> never thought too much about it, but currently, template inheritance
> and includes don't work together at all:
>
> * Blocks included in a parent template cannot be overwritten in a
> child template.
> * Blocks from an include in a child template cannot overwrite blocks
> in a parent template.

I've got a workaround for that which works pretty well:

http://code.djangoproject.com/wiki/ExtendingTemplates

Cheers,

Simon

Michael Elsdörfer

unread,
Sep 18, 2008, 6:37:00 PM9/18/08
to Django developers
> I've got a workaround for that which works pretty well:
>
> http://code.djangoproject.com/wiki/ExtendingTemplates

Hm I might be misunderstanding, but it seems you're trying to
workaround the situation where two templates might not be uniquely
addressable. I can't quite make the connection to the issue I was
referring to (the fact that block-tags inside a template that was
included using {% include %} are ignored by the inheritance-
mechanism).

Michael

SmileyChris

unread,
Sep 18, 2008, 6:58:21 PM9/18/08
to Django developers
I think you missed the point, Simon.

Michael isn't talking about self-referencing extending, he's talking
about blocks in statically included templates ({% include "bit.htm"
%}).

I can't see a reason why at least static blocks shouldn't be exposed
as self.nodelist

Simon Willison

unread,
Sep 19, 2008, 4:42:32 AM9/19/08
to Django developers
On Sep 18, 11:58 pm, SmileyChris <smileych...@gmail.com> wrote:
> I think you missed the point, Simon.
>
> Michael isn't talking about self-referencing extending, he's talking
> about blocks in statically included templates ({% include "bit.htm"
> %}).

Ah yes, sorry - misunderstood the original e-mail.

Cheers,

Simon
Reply all
Reply to author
Forward
0 new messages