Opened 16 years ago

Closed 14 years ago

Last modified 13 years ago

#7817 closed (fixed)

{% include "tpl" with foo=x bar=y %}

Reported by: Johannes Dollinger Owned by: Chris Beaven
Component: Template system Version: dev
Severity: Keywords:
Cc: michael@…, lrekucki@…, Gonzalo Saavedra, mlouro@…, Mikhail Korobov Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is a simple but useful syntax extension. The separator could also be "and": {% with foo as x and bar as y %}.

{% include "tpl" with foo as x %} would be equivalent to {% with foo as x %}{% include "tpl"%}{% endwith %}, which is more transparent that a trivial inclusion_tag.

Use a common parser for include, with and possibly blocktrans.

Attachments (4)

7817-with-tag-multiple-bindings.2.diff (4.9 KB ) - added by Antti Kaihola 16 years ago.
[patch] Allows multiple bindings in the {% with %} tag. Python <=2.4 compatible with tests and documentation.
7817_extended_with_and_include.diff (8.7 KB ) - added by Łukasz Rekucki 14 years ago.
Improved patch against r13716.
ticket7817_r13997.diff (6.5 KB ) - added by Łukasz Rekucki 14 years ago.
Patch without "with" related stuff. Added docs and one more test.
7817.diff (6.7 KB ) - added by Chris Beaven 14 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 by Eric Holscher, 16 years ago

milestone: post-1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 by miracle2k, 16 years ago

Cc: michael@… added

comment:3 by Johannes Dollinger, 16 years ago

#9456 has a patch for {% with foo as x and bar as y ... %}.

comment:4 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

by Antti Kaihola, 16 years ago

[patch] Allows multiple bindings in the {% with %} tag. Python <=2.4 compatible with tests and documentation.

comment:5 by Antti Kaihola, 16 years ago

Has patch: set

I took eibaan's patch from #9456 (which is marked as a duplicate of this ticket) and made the following improvements:

  • added Python 2.3 and 2.4 compatibility by not using 2.5's any() built-in function
  • now raises an error if something else than "and" is used as a delimiter
  • added documentation
  • added three more tests

The patch is based on r10188.

comment:6 by Antti Kaihola, 16 years ago

To clarify, the patch above does not implement the {% include "tpl" with foo as x and bar as y %} syntax. It only extends the {% with %} tag in a backwards-compatible way. I also chose "and" as the delimiter since I felt it fits existing syntax better than a comma.

If core devs decide the suggested {% include %} syntax is a good idea, I'm willing to extend the patch.

comment:7 by Łukasz Rekucki, 14 years ago

Cc: lrekucki@… added
Owner: changed from nobody to Łukasz Rekucki

comment:8 by Gonzalo Saavedra, 14 years ago

Cc: Gonzalo Saavedra added

by Łukasz Rekucki, 14 years ago

Improved patch against r13716.

comment:9 by Łukasz Rekucki, 14 years ago

Attached patch extends both "with" and "include" tags with following syntax:

{% with foo as x and bar as y %}
{% include "partial.html" with label="Hello world" foo=baz %}

After some thought I decided to use keyword style arguments in include. Other tags use "as" to introduce new variables to the current scope. Using it here could give a false impression that the template is actually rendered to a variable (like the url tag).

comment:10 by Chris Beaven, 14 years ago

Patch needs improvement: set
Summary: {% with foo as x, bar as y %}, {% include "tpl" with foo as x, bar as y %}{% include "tpl" with foo=x bar=y %}

There are two separate feature requests here, one for {% with %} and one for {% include %}.

Although similar, they are separate requests which may have separate resolutions. Let's use this one for {% include %} - I'm reopening #9456 to track {% with %}.
I'm more keen on this one, ambivalent about the other one.

comment:11 by anonymous, 14 years ago

Cc: mlouro@… added

by Łukasz Rekucki, 14 years ago

Attachment: ticket7817_r13997.diff added

Patch without "with" related stuff. Added docs and one more test.

comment:12 by Łukasz Rekucki, 14 years ago

Patch needs improvement: unset

by Chris Beaven, 14 years ago

Attachment: 7817.diff added

comment:13 by Chris Beaven, 14 years ago

milestone: 1.3
Owner: changed from Łukasz Rekucki to Chris Beaven
Status: newassigned

comment:14 by Mikhail Korobov, 14 years ago

Cc: Mikhail Korobov added

comment:16 by Jannis Leidel, 14 years ago

Something like this seems more reasonable to me: {{{
{% include "name_snippet.html" with "Joe" as name and "Hello" as greeting %}
}}}

comment:17 by Russell Keith-Magee, 14 years ago

Triage Stage: Design decision neededAccepted

Following a BDFL pronouncement, this has been accepted, using the {% include "templ.html" with foo=x bar=y %} syntax. Related ticket: #9456.

comment:18 by Chris Beaven, 14 years ago

Resolution: fixed
Status: assignedclosed

(In [14922]) Fixes #7817 and #9456.

  • The include tag now has a 'with' option to include to provide extra context vairables to the included template.
  • The include tag now has an 'only' option to exclude the current context when rendering the included template.
  • The with tag now accepts multiple variable assignments.
  • The with, include and blocktrans tags now use a new keyword argument format for variable assignments (e.g. {% with foo=1 bar=2 %}).

comment:19 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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