Hey, this pattern are nested decorators in templates.

Most people here are probably against having nested template tags with
the same name. I think it does only make sense when there's somehow a
distinction between a
"placeholder" and "content". Because otherwise, templates don't know
which block in the parent template need to be chosen when several have
the same name. (You'd say the most inner block, but that's not obvious
-- I think.)

I once wrote a {% decorate %} template tag for a little similar
behaviour.
https://github.com/citylive/django-template-tags/blob/master/src/django_template_tags/templatetags/decorate.py

That would become:

template.html:
{% decorate (either "a.html" or "b.html") %}
middle
{% enddecorate %}


b.html:
{% decorate "a.html" %}
left {{ decorator.content }} right
{% enddecorate %}

a.html:
first {{ decorator.content }} last

Hope that helps.
I once tried to get this into django trunk, but did not yet got
accepted.


On 29 avr, 02:11, amagee <ama...@gmail.com> wrote:
> I sometimes run into a situation where I want a template to be able to
> extend from one of a set of possible base templates, which I achieve
> by passing a "base_template" variable in the context to the {% extends
> %} tag.  Where this gets stuck, though, is if one of the possible
> bases extends one of the other possible bases.
>
> For example:
>
> base_a.html:
> first {% block content %}{% endblock %} last
>
> base_b.html
> {% extends "base_a.html" %}
> {% block content %}left {% ??? %} right{% endblock %}
>
> template.html
> {% extends (either "base.a.html" or "base_b.html") %}
> {% block content %}middle{% endblock %}
>
> I'd like to be able to code template.html so that if it extends
> base_a.html, the result is "first middle last", but if it extends
> "base_b.html", the result is "first left middle right last".
>
> I _think_ it would make sense to implement this with an improvement to
> the semantics of the {% block %} tag, starting by allowing nested tags
> with the same name.
>
> If base_b.html were:
> {% extends "base_a.html" %}
> {% block content %}left {% block content %}{% endblock %} right{%
> endblock %}
>
> Then the {% block content %} in template.html could override the
> _inner_ block in base_b.html.  I think this behaviour is pretty
> logical and consistent, unless I've missed something.  I've hacked
> around a bit with loader_tags.py but I'm finding it quite difficult to
> get what I want with my limited understanding of how it works.
>
> Do people think this idea makes sense?  Is it worth taking the time to
> write a patch for it?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to