#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)
Change History (23)
comment:1 by , 16 years ago
milestone: | → post-1.0 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 16 years ago
Cc: | added |
---|
comment:3 by , 16 years ago
by , 16 years ago
Attachment: | 7817-with-tag-multiple-bindings.2.diff added |
---|
[patch] Allows multiple bindings in the {% with %} tag. Python <=2.4 compatible with tests and documentation.
comment:5 by , 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 , 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 , 14 years ago
Cc: | added |
---|---|
Owner: | changed from | to
comment:8 by , 14 years ago
Cc: | added |
---|
by , 14 years ago
Attachment: | 7817_extended_with_and_include.diff added |
---|
Improved patch against r13716.
comment:9 by , 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 , 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 , 14 years ago
Cc: | added |
---|
by , 14 years ago
Attachment: | ticket7817_r13997.diff added |
---|
Patch without "with" related stuff. Added docs and one more test.
comment:12 by , 14 years ago
Patch needs improvement: | unset |
---|
by , 14 years ago
comment:13 by , 14 years ago
milestone: | → 1.3 |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:14 by , 14 years ago
Cc: | added |
---|
comment:15 by , 14 years ago
My latest changes can be found here: http://github.com/SmileyChris/django/compare/master...7817-include-with
comment:16 by , 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 , 14 years ago
Triage Stage: | Design decision needed → 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 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → 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 %}
).
#9456 has a patch for
{% with foo as x and bar as y ... %}
.