Opened 5 years ago

Closed 5 years ago

Last modified 5 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: UI/UX:

Description

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 page.name|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)

setcontext.py (893 bytes) - added by mitar 5 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years ago by mitar

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 5 years ago by mitar

comment:2 Changed 5 years ago by mitar

  • Has patch set

comment:3 Changed 5 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

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 5 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 5 years ago by ericholscher

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": http://code.djangoproject.com/wiki/Version1.2Features#Lowpriority

comment:6 Changed 5 years ago by russellm

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