Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#14078 closed (wontfix)

Template tag to set context variable to output from tag's rendered content

Reported by: Mitar Owned by: nobody
Component: Template system Version: 1.2
Severity: Keywords:
Cc: mmitar@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have made a tag which renders its content and puts the result into the context variable. I believe it would be useful to include it into built-in template tags. The syntax is `{% setcontext as var_name %}...{% endsetcontext %}.

It is most useful when you wan to generate some translated (localized) HTML code and pass it to other tags or filters for processing (especially multiple times):

{% setcontext as heading %}
{% url view_page page.get_current_id as page_url %}
{% blocktrans with|default:_("untitled") as page_name %}
Edit {{ page_element }} for Page <a href="{{ page_url }}">{{ page_name }}</a>
{% endblocktrans %}
{% endsetcontext %}

And then you can use {{ heading|striptags }} for HTML title, {% heading 3 heading %} for rendering heading (a custom template tag) with level 3 and above content.

Attachments (1) (893 bytes) - added by Mitar 11 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 11 years ago by Mitar

Ah, I forgot, this custom heading tag then renders heading so that it is possible to have anchor set to it. So you get localized anchors for free. ;-)

Changed 11 years ago by Mitar

Attachment: added

comment:2 Changed 11 years ago by Mitar

Has patch: set

comment:3 Changed 11 years ago by Russell Keith-Magee

Resolution: wontfix
Status: newclosed

This kind of tag has been omitted from django's template language on purpose. Django's template language is not intended to be a Turing complete programming language; a "set variable" tag only serves to encourage the introduction of view logic into the template (where it doesn't belong).

comment:4 Changed 11 years ago by Mitar

I agree that view logic should not be put in the template. But designing HTML and injecting translation to be used in other blocks does belong to template language.

Maybe it would be enough to extend blocktrans tag with {% blocktrans as var_name with ... %} where translated string would be put into context? This is in fact the problem I am trying to solve with my tag, I just generalized it also to other situations.

comment:5 Changed 11 years ago by Eric Holscher

This is essentially a duplicate of #6378 -- I'm wondering if that one should be closed as wontfix as well? It was included on the 1.2 feature list as a "low priority":

comment:6 Changed 11 years ago by Russell Keith-Magee

I disagree that this ticket and #6378 are the same. #6378 is about setting a block of content, not a single variable (although you could obviously use #6378 to set a single variable). I can get behind the root idea that #6378 is proposing; this ticket strikes me as an explicit attempt to put view logic in the template.

Note: See TracTickets for help on using tickets.
Back to Top