templates: override inner nested block

557 views
Skip to first unread message

anentropic

unread,
May 24, 2015, 7:28:08 AM5/24/15
to django-d...@googlegroups.com
Currently there is a problem (correct me if wrong) when you need to override the content of an inner nested block.

{% block parent %}
  some stuff
  {% block child %}
     child stuff
  {% endblock %}
  some other stuff
{% endblock %}

if you are in a template which extends the one above and you want to override just the content of 'child' block you are stuck

if you just do:

  {% block child %}
     new child stuff
  {% endblock %}

...you get two of the 'child' block, because you haven't overridden the parent block, so there's another one from there.

But then if you need to override the parent block you have to copy and paste all the content that you don't want to change, since {{ block.super }} can't be used (the part you want to override is going to be reproduced by the super).

Possible implementation difficulties aside, is it a namespacing problem? Do we need a syntax for saying like:

  {% block parent:child %}
     new child stuff
  {% endblock %}

...then Django can know you want to just override the inner nested block, with everything else from the parent blocks being reproduced verbatim (without having to copy and paste it breaking DRY)


Curtis Maloney

unread,
May 24, 2015, 8:50:10 PM5/24/15
to django-d...@googlegroups.com
Could you provide a solid, minimal example of how you get this case?

I have seen duplicate rendering of nested blocks happen in the past, and it was because in the resulting template, two  blocks wind up with the child defined in them [this actually lead to me writing a {% reuse %} tag to take advantage of this :)]

--
Curtis


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/26e7d546-0a49-408b-8480-b8d6b4aba608%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen J. Butler

unread,
May 25, 2015, 6:05:57 AM5/25/15
to django-d...@googlegroups.com
On Sun, May 24, 2015 at 6:28 AM, anentropic <pa...@blues.co.nz> wrote:
> Currently there is a problem (correct me if wrong) when you need to override
> the content of an inner nested block.
>
> {% block parent %}
> some stuff
> {% block child %}
> child stuff
> {% endblock %}
> some other stuff
> {% endblock %}
>
> if you are in a template which extends the one above and you want to
> override just the content of 'child' block you are stuck
>
> if you just do:
>
> {% block child %}
> new child stuff
> {% endblock %}
>
> ...you get two of the 'child' block, because you haven't overridden the
> parent block, so there's another one from there.

That's not the behavior I see. Maybe you have some other error in your code?

https://uofi.box.com/s/hlxb3qu575t7exx6r4q3aa3w47ya5362

anentropic

unread,
May 25, 2015, 9:42:21 AM5/25/15
to django-d...@googlegroups.com
Hmm, I can't reproduce the problem I described above, either with your code example or my own full code. Must be my own error.

Apologies for the time wasting!
Reply all
Reply to author
Forward
0 new messages