Proposal: Optional {% default %} clause for the {% for %} template tag

0 views
Skip to first unread message

Jannis Leidel

unread,
Oct 28, 2008, 7:46:59 PM10/28/08
to Django developers
Hi there,

I propose a feature which we already discussed a bit before 1.0 in
ticket #6398 [1]. It's about a small enhancement to the "for" template
tag that would allow users to specify a default item to be shown in
case the given iterable is empty.

Currently that's also possible with this:

{% if grocery_list %}
{% for item in grocery_list %}
{{ item }}
{% endfor %}
{% else %}
Nothing to buy.
{% endif %}

The proposed feature would allow this:

{% for item in grocery_list %}
{{ item }}
{% default %}
Nothing to buy.
{% endfor %}

I believe it would allow template authors to produce cleaner templates
since it prevents repetition of if statements a lot.


Cheers,
Jannis

1: http://code.djangoproject.com/ticket/6398

oggie rob

unread,
Oct 28, 2008, 8:18:14 PM10/28/08
to Django developers
> {% for item in grocery_list %}
>   {{ item }}
> {% default %}
>   Nothing to buy.
> {% endfor %}

Please, though - use {% else %}. Its totally clear what its referring
to and else doesn't mean squat unless you see what the if (and in this
case, for) test is anyway, so I don't think this would be confusing
(after all, this isn't python).
(Also, if you want to avoid confusion don't use a keyword that is
located within another language's looping construct :)

-rob

SmileyChris

unread,
Oct 28, 2008, 9:09:55 PM10/28/08
to Django developers
Note, that this is actually a dupe of #3481.
Regarding, {% else %}, see what Malcolm said about it -
http://code.djangoproject.com/ticket/3481#comment:2

Calvin Spealman

unread,
Oct 28, 2008, 9:43:12 PM10/28/08
to django-d...@googlegroups.com
Please dont use else, because {%for%} matches python's for loop and that supports an else clause which does not operate like this. If the same keyword is used, it should behave the same.


--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy

Ivan Sagalaev

unread,
Oct 29, 2008, 3:46:37 AM10/29/08
to django-d...@googlegroups.com
Jannis Leidel wrote:
> The proposed feature would allow this:
>
> {% for item in grocery_list %}
> {{ item }}
> {% default %}
> Nothing to buy.
> {% endfor %}

As of coloring the bikeshed, I prefer {% empty %} over {% default %} and
{% else %}.

Ned Batchelder

unread,
Oct 29, 2008, 8:32:49 PM10/29/08
to django-d...@googlegroups.com
But this does operate the same as the Python for/else, no?
>>> for i in []:
...  print "boo"
... else:
...  print "foo"
...
foo
>>>

--Ned.
http://nedbatchelder.com
-- 
Ned Batchelder, http://nedbatchelder.com

Calvin Spealman

unread,
Oct 29, 2008, 8:44:39 PM10/29/08
to django-d...@googlegroups.com
The else clause executes unless a loop is broken (that is, with the break or obviously by raising an exception), and executes after a loop ends normally.

>>> for i in "abc":
...   pass
... else:
...   print "else"
else

Ned Batchelder

unread,
Oct 29, 2008, 9:07:32 PM10/29/08
to django-d...@googlegroups.com
Yes, you are right!

--Ned.

Dave Smith

unread,
Oct 29, 2008, 9:48:18 PM10/29/08
to django-d...@googlegroups.com
I'm wondering who that's going to confuse. It's very clear that the template language
isn't Python, so I'd think it'd make the most sense to use a keyword that makes sense
within the context of the template language. I'd think that either 'else' or 'ifnone' are the
most memorable/readable. 'default' connotes "unless we override ...", which isn't what's
going on here.

+1 for else

Calvin Spealman

unread,
Oct 29, 2008, 10:07:33 PM10/29/08
to django-d...@googlegroups.com
On Wed, Oct 29, 2008 at 9:48 PM, Dave Smith <davew...@gmail.com> wrote:
I'm wondering who that's going to confuse. It's very clear that the template language
isn't Python, so I'd think it'd make the most sense to use a keyword that makes sense
within the context of the template language. I'd think that either 'else' or 'ifnone' are the
most memorable/readable. 'default' connotes "unless we override ...", which isn't what's
going on here.

+1 for else

The template language may not be sold as "python in html" but there are still obvious relationships between the constructs the two share. Breaking assumptions then simply isn't good for anyone involved.

+1 for a default, -1 for calling it else

Steve Holden

unread,
Oct 29, 2008, 11:05:07 PM10/29/08
to django-d...@googlegroups.com
Calvin Spealman wrote:
> On Wed, Oct 29, 2008 at 9:48 PM, Dave Smith <davew...@gmail.com
> <mailto:davew...@gmail.com>> wrote:
>
> I'm wondering who that's going to confuse. It's very clear that
> the template language
> isn't Python, so I'd think it'd make the most sense to use a
> keyword that makes sense
> within the context of the template language. I'd think that either
> 'else' or 'ifnone' are the
> most memorable/readable. 'default' connotes "unless we override
> ...", which isn't what's
> going on here.
>
> +1 for else
>
>
> The template language may not be sold as "python in html" but there
> are still obvious relationships between the constructs the two share.
> Breaking assumptions then simply isn't good for anyone involved.
>
> +1 for a default, -1 for calling it else
>
+1 to both the +1 and the -1. It *will* confuse Python programmers who
also happen to write templates, and personally I think {% empty %}
expresses the meaning much more clearly to all classes of user.

regards
Steve

Jacob Kaplan-Moss

unread,
Oct 29, 2008, 11:41:11 PM10/29/08
to django-d...@googlegroups.com
I'm with Steve::

{% for %} ... {% default %} .. {% endfor %}

... seems best.

Jacob

Steve Holden

unread,
Oct 30, 2008, 12:17:40 AM10/30/08
to django-d...@googlegroups.com
Agreement is great, but my suggestion was actually

{% for %} ... {% empty %} ... {% endfor %}

I feel this says more than "default", which requires you to think about
when the default value is required.

regards
Steve

dc

unread,
Oct 30, 2008, 9:55:17 AM10/30/08
to Django developers
How about

{% for item in items %}
{% otherwise %}
{% endfor %}

form?

And -1 for the {% else %}.

Mike Panchenko

unread,
Oct 30, 2008, 1:06:13 PM10/30/08
to django-d...@googlegroups.com
+1 for {% empty %}

I also think this would be very useful

Antoni Aloy

unread,
Oct 30, 2008, 1:29:50 PM10/30/08
to django-d...@googlegroups.com
2008/10/30 Mike Panchenko <m...@mihasya.com>:

> +1 for {% empty %}
>
> I also think this would be very useful
>
> On Thu, Oct 30, 2008 at 6:55 AM, dc <dmm...@gmail.com> wrote:
>>
>> How about
>>
>> {% for item in items %}
>> {% otherwise %}
>> {% endfor %}
>>

+1 to the otherwise tag :)


--
Antoni Aloy López
Blog: http://trespams.com
Site: http://apsl.net

Eric Holscher

unread,
Oct 30, 2008, 1:59:44 PM10/30/08
to django-d...@googlegroups.com
{% for color in patches %}
Bikeshed: {{ color }}
{% default %}
Person who writes the patch decides
{% endfor %}

I like empty/default or else. Use else if your main target is python people. Use empty or default if your targetting it to designers. It really doesn't matter which..
--
Eric Holscher
Web Developer at The World Company in Lawrence, Ks
http://www.ericholscher.com
er...@ericholscher.com

Jannis Leidel

unread,
Oct 30, 2008, 6:25:53 PM10/30/08
to django-d...@googlegroups.com
> {% for color in patches %}
> Bikeshed: {{ color }}
> {% default %}
> Person who writes the patch decides
> {% endfor %}

Yay, I decide! Err, decided.. :) There is a patch attached to ticket
#6398 that implements "default".

Cheers,
Jannis

Reply all
Reply to author
Forward
0 new messages