{% with %} tag

1 view
Skip to first unread message

SmileyChris

unread,
Mar 24, 2007, 2:42:08 AM3/24/07
to Django developers
For a while I've been thinking that it would be nice to have a tag so
you could re-use an expression in a template.
I did one up and put it on http://www.djangosnippets.org/snippets/132/

Is this useful enough for core?

Baptiste

unread,
Mar 24, 2007, 3:01:03 PM3/24/07
to Django developers
For me, yes, but I miss the "and" :
{% with article.name as name and article.title as title %}
But it is an useful tag.

On Mar 24, 7:42 am, "SmileyChris" <smileych...@gmail.com> wrote:
> For a while I've been thinking that it would be nice to have a tag so
> you could re-use an expression in a template.

> I did one up and put it onhttp://www.djangosnippets.org/snippets/132/

Aidas Bendoraitis

unread,
Mar 24, 2007, 4:00:01 PM3/24/07
to django-d...@googlegroups.com
Wouldn't it be more convenient not to have the closing {% endwith %}?
Once you defined a variable, you could use it anywhere in the template
without taking care about closing tags.

Regards,
Aidas Bendoraitis [aka Archatas]

SmileyChris

unread,
Mar 24, 2007, 4:22:31 PM3/24/07
to Django developers

On Mar 25, 8:00 am, "Aidas Bendoraitis" <aidas.bendorai...@gmail.com>
wrote:


> Wouldn't it be more convenient not to have the closing {% endwith %}?

Perhaps, but rather than "dirtying" up the context, this keeps the
variable nice and encapsulated.

> On 3/24/07, Baptiste <baptiste.gou...@gmail.com> wrote:
> > For me, yes, but I miss the "and" :
> > {% with article.name as name and article.title as title %}
> > But it is an useful tag.

If you wanted to do this, you could just open another with tag. Apart
from a saving few characters, is there an advantage to complicating
the tag?

Baptiste

unread,
Mar 25, 2007, 11:48:31 AM3/25/07
to Django developers
{% with article.title as title %}
{% with article.url as url %}
{% with article.date as date %}
{% with article.category.title as category %}
...
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}

is just definitely awful.

;-)

Brian Beck

unread,
Mar 25, 2007, 3:58:39 PM3/25/07
to Django developers
On Mar 24, 3:01 pm, "Baptiste" <baptiste.gou...@gmail.com> wrote:
> For me, yes, but I miss the "and" :
> {% with article.name as name and article.title as title %}
> But it is an useful tag.

If this were to go in, I think comma-delimiting multiple "assignments"
would make more sense than using 'and':

{% with article.name as name, article.title as title %}

This mirrors Python's import statement:

import math as x, urllib as y, os as z

SmileyChris

unread,
Mar 25, 2007, 5:08:25 PM3/25/07
to Django developers
And just to make it clear, the main point of this patch is so that
templates can benefit from the internal caching going on. Such as:

{% for product in products %}
{{ product }}
{% with product.purchases.count as purchases %}({{ purchases }}
purchase{{ purchases|pluralize }}){% endwith %}
{% endfor %}

Arvin Schnell

unread,
Mar 26, 2007, 5:17:07 AM3/26/07
to django-d...@googlegroups.com

I would like to see a tag like that. The fact that snippet #9
and #124 deal with the same problem shows the people need/want
it.

Django should have a tag like that before everybody implements
their own different solution.

cu Arvin

--
Arvin Schnell, <asch...@suse.de>
Software Engineer, Research & Development
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)

Malcolm Tredinnick

unread,
Mar 26, 2007, 5:32:25 AM3/26/07
to django-d...@googlegroups.com
On Mon, 2007-03-26 at 11:17 +0200, Arvin Schnell wrote:
> On Sat, Mar 24, 2007 at 06:42:08AM -0000, SmileyChris wrote:
> >
> > For a while I've been thinking that it would be nice to have a tag so
> > you could re-use an expression in a template.
> > I did one up and put it on http://www.djangosnippets.org/snippets/132/
> >
> > Is this useful enough for core?
>
> I would like to see a tag like that. The fact that snippet #9
> and #124 deal with the same problem shows the people need/want
> it.

Snippet #9 is quite different. It's providing arbitrary expression
computation, which is out of scope for the core templating language, but
is a useful third-party add-on for some people. Limodou been maintaining
that for quite a while now.

Chris's snippet #132 looks a bit nicer than #124. The "ifvalue"
construction in #124 looks like it's trying to do two or three things at
once. If I wanted that, I'd use Perl.

I'm personally a bit indifferent (which is not the same as "against"
before anybody gets riled up) to the arguments about "saving typing",
since all editors have decent auto-complete these days and it's just a
slightly longer name if you don't have aliasing. However, on a related
topic, it's easy when writing things like object.person.count to create
repetitive queries (there's no reuse of that queryset if you type the
same thing later). So I was thinking in idle moments today about how
possible it would be to add transparent caching to templates. It doesn't
seem to be impossible, although I need to sit down with a pen and paper
and scratch my head a bit more to work out the details (for example, you
need to have a way to mark some tags/filters as volatile so that
something like the "random" filter doesn't behave identically to the
hypothetical "always the same" filter).

Regards,
Malcolm


Jacob Kaplan-Moss

unread,
Mar 26, 2007, 11:07:09 AM3/26/07
to django-d...@googlegroups.com
On 3/26/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
> However, on a related
> topic, it's easy when writing things like object.person.count to create
> repetitive queries (there's no reuse of that queryset if you type the
> same thing later). So I was thinking in idle moments today about how
> possible it would be to add transparent caching to templates.

I actually the the {% with %} tag is a better choice for this --
"explicit is better than implicit" -- and after fooling with {% with
%} a bit, I'd say I'm +0 on this. Barring any serious objects (from
Malcolm or anyone) I think this should go in.

Chris: can you work up a patch in case we decide to check this in?

Jacob

SmileyChris

unread,
Mar 26, 2007, 4:49:36 PM3/26/07
to Django developers
On Mar 27, 3:07 am, "Jacob Kaplan-Moss" <jacob.kaplanm...@gmail.com>
wrote:

> Chris: can you work up a patch in case we decide to check this in?

Sure: http://code.djangoproject.com/ticket/3826

Jacob Kaplan-Moss

unread,
Mar 26, 2007, 6:06:57 PM3/26/07
to django-d...@googlegroups.com
On 3/26/07, SmileyChris <smile...@gmail.com> wrote:
> Sure: http://code.djangoproject.com/ticket/3826

Sweet; thanks.

I'm gonna wait a couple of days for any objections from Malcom/Adrian,
but unless I hear any I'll add this shortly.

Jacob

Malcolm Tredinnick

unread,
Mar 26, 2007, 8:04:24 PM3/26/07
to django-d...@googlegroups.com

I have no objections. I'm +0 on the feature.

Cheers,
Malcolm

Baptiste

unread,
Mar 28, 2007, 9:07:10 AM3/28/07
to Django developers
It has been commited ; that is cool, but without any "and" or "," that
is not cool.
Like in my example, it is idiot to have to open a tag by variable...
no?

On Mar 27, 2:04 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:


> On Mon, 2007-03-26 at 17:06 -0500, Jacob Kaplan-Moss wrote:

mich...@gmail.com

unread,
Apr 2, 2007, 2:14:23 PM4/2/07
to Django developers
Hi guys!

I've done a patch to the "with" tag be able to accept multiple
assignments without the need to nest many with tags, look the
following example:

{% with complicated.var.name1 as name1 and complicated.var.name2 as
name2 %}
{{ name1 }}, {{ name2 }}
{% endwith %}

I reopened the proper tag: http://code.djangoproject.com/ticket/3826
and attached the patch.
The people here talked about separate many assigments with a comma or
an and, I prefer the "and" because it was easy to implement, if you
like the comma I can change it.

Best regards to you!

Reply all
Reply to author
Forward
0 new messages