Django

Code

Ticket #6378 (new)

Opened 3 years ago

Last modified 9 months ago

Capture arbitrary output as a template variable

Reported by: kcarnold Assigned to: nobody
Milestone: Component: Template system
Version: SVN Keywords:
Cc: kenneth.arnold@gmail.com Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

I'd like to suggest including http://www.djangosnippets.org/snippets/545/ in Django because of its generality and simplicity, compared to adding the equivalent functionality to each individual tag, or trying to use the block tag for this purpose as has been suggested.

Attachments

6378-into.diff (3.8 kB) - added by ericholscher on 02/28/09 16:36:48.
Initial patch that is a bit rough.

Change History

01/14/08 15:09:56 changed by Simon Greenhill <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests changed.
  • needs_docs changed.

03/03/08 07:44:46 changed by mtredinnick

Search out the discussions from last year on django-dev where we were talking about how to extend the "with" tag to work with tags, not just variables. I don't think any real consensus was reached, but something that is a block-based version of "with" (and spelt more or less the same) is much more likely to have legs here. Making people learn yet another tag that isn't related to the existing thing that works like this ("with") isn't good API.

08/04/08 15:09:34 changed by kcarnold

  • cc set to kenneth.arnold@gmail.com.

02/28/09 15:15:47 changed by ericholscher

@Malcolm: I couldn't find the discussion from django-dev, "with" is stripped out by google's search. Anyway, here is an idea I've had about this functionality.

How about implementing this as an optional "into" (colored appropriately) where you could have a statement along the lines of

{% with person.get_absolute_url as main_url into content %}
check out my page: {{ main_url }}
{% endwith %}

This would then instead of outputting the included block into the template, stuff it into the content variable.

However, this doesn't cover the situation where you don't want to put a new variable into the context, but you simply want to put that output into a variable. Something along the lines of {% with [None|magic_word] into content %} was the best I could come up with.

I think that capturing the output of a block is a different operation than including a variable into the inner block's context. Maybe a better solution would be to have a {% withblock as content %} tag that is a counterpart to the "with" tag.

It feels like this functionality shouldn't go in two places, so maybe "withblock" would also have the ability to set the context variables inside. Seems bad to have the functionality in two places either way. The best situation would probably be a sane way to define the capture syntax on the "with" tag, without settings the context variable.

02/28/09 16:23:34 changed by ericholscher

I wrote up a patch with this code in it. It has tests, but the test:

            'with04': ('{{ content }}-{% with dict.key as key into content %}{{ key }}{% endwith %}-{{ content }}', {'dict': {'key': 50}}, '--50'),

Is failing only when

Template test (TEMPLATE_STRING_IF_INVALID='INVALID'): with04 -- FAILED. Expected '--50', got u'50--50'

When the TEMPLATE_STRING_IF_INVALID="", it works fine and only inserts into the context afterwards. When it is set to INVALID, it appears to break. I don't really understand this but will investigate further.

02/28/09 16:36:48 changed by ericholscher

  • attachment 6378-into.diff added.

Initial patch that is a bit rough.

10/16/09 21:29:21 changed by ericholscher

10/29/09 15:46:17 changed by nathan

This would be a great addition to the template language. All to often I add "wrapper" blocks to cancel out HTML so its style doesn't effect the layout. This leads to added template code in child templates.

I've always seen {% with %} more for reassigning variables but if we can augment it to hand code snippets that would be nice but I don't mind learning new tags :)

Scenario: http://gist.github.com/221803

10/29/09 21:56:58 changed by jtinsky

I agree this would be helpful in a wide variety of situations. "{% with %}" doesn't always get the job done.

12/15/09 17:37:42 changed by nathan

Another use-case that would be handy if you wanted to reuse block content:

{% capture as title %}
  {% block title %}{% endblock %}
{% endcapture %}

<html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>{{ title }}</h1>
    </body>
</html>

Add/Change #6378 (Capture arbitrary output as a template variable)




Change Properties
Action