Code

Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#7817 closed (fixed)

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

Reported by: emulbreh Owned by: SmileyChris
Component: Template system Version: master
Severity: Keywords:
Cc: michael@…, lrekucki@…, gonz, mlouro@…, kmike Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 akaihola 5 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 lrekucki 4 years ago.
Improved patch against r13716.
ticket7817_r13997.diff (6.5 KB) - added by lrekucki 4 years ago.
Patch without "with" related stuff. Added docs and one more test.
7817.diff (6.7 KB) - added by SmileyChris 3 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 Changed 6 years ago by ericholscher

  • milestone set to post-1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 6 years ago by miracle2k

  • Cc michael@… added

comment:3 Changed 5 years ago by emulbreh

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

comment:4 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Changed 5 years ago by akaihola

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

comment:5 Changed 5 years ago by akaihola

  • 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 Changed 5 years ago by akaihola

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 Changed 4 years ago by lrekucki

  • Cc lrekucki@… added
  • Owner changed from nobody to lrekucki

comment:8 Changed 4 years ago by gonz

  • Cc gonz added

Changed 4 years ago by lrekucki

Improved patch against r13716.

comment:9 Changed 4 years ago by lrekucki

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 Changed 4 years ago by SmileyChris

  • Patch needs improvement set
  • Summary changed from {% with foo as x, bar as y %}, {% include "tpl" with foo as x, bar as y %} to {% 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 Changed 4 years ago by anonymous

  • Cc mlouro@… added

Changed 4 years ago by lrekucki

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

comment:12 Changed 3 years ago by lrekucki

  • Patch needs improvement unset

Changed 3 years ago by SmileyChris

comment:13 Changed 3 years ago by SmileyChris

  • milestone set to 1.3
  • Owner changed from lrekucki to SmileyChris
  • Status changed from new to assigned

comment:14 Changed 3 years ago by kmike

  • Cc kmike added

comment:15 Changed 3 years ago by SmileyChris

comment:16 Changed 3 years ago by jezdez

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

comment:17 Changed 3 years ago by russellm

  • Triage Stage changed from Design decision needed to Accepted

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

comment:18 Changed 3 years ago by SmileyChris

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.