Django forloop.last equivalent

65 views
Skip to first unread message

Sylvain

unread,
Oct 20, 2009, 9:36:48 AM10/20/09
to Tornado Web Server
Hi,

What is the best solution for the "forloop.last" keyword (i.e : {% if
forloop.last %}end{% endif %} ).
(same thing with : forloop.first)

Currently, for : forloop.last, I've done this. (not very elegant, I
think)

{% set l = len(mylist) %}
{% set i = 0 %}
{% for ml in mylist %}
{% set i += 1 %}
{{ ml }} {% if l != i %},{% end %}
{% end %}

Benjamin Golub

unread,
Oct 20, 2009, 9:52:41 AM10/20/09
to python-...@googlegroups.com
Since you can just use Python in Tornado templates the elegant solution is to use enumerate:

{% set last = len(mylist) - 1 %}
{% for i, value in enumerate(mylist) %}
  {{ value }}{% if i != last %},{% end %}
{% end %}

But it looks like you are trying to comma separate a list of values. You can use locale.list for a similar result: http://github.com/facebook/tornado/blob/master/tornado/locale.py#L305

In that case you could just write:

{{ local.list(mylist) }}

And given the list ["foo", "bar", "baz"] it would output: "foo, bar, and baz".

Given ["foo", "bar"] it would output "foo and bar".

It's very useful.

Andrew Gwozdziewycz

unread,
Oct 20, 2009, 10:08:39 AM10/20/09
to python-...@googlegroups.com
One thing that might be worth doing is create a class that implements
the iterator
protocol and keeps track of these things. So, rather than implement
all of this logic in
the template for common idioms...

{% set wrapped = ListLoopExtras(somelist) %}
{% for item in wrapped %}
{% if wrapped.last %}
{{ item }} is the last thing.
{% elif wrapped.odd %}
{{ item }} is at an odd index
...


{% end %}
{% end %}

These idioms are extremely common in templating, and are easy to
provide as extras
to the template language, without requiring core changes to Python.

I'd recommend that before we ask Bret and the tornado crew to include
something like
this, we build it and see how it feels so that our recommendations are
backed by real
use, rather than a whim.

Thoughts?

--
http://www.apgwoz.com

Sylvain

unread,
Oct 20, 2009, 11:50:22 AM10/20/09
to Tornado Web Server
Thank you all.

It could be great to have a template_addon module in the core for all
this basic tasks + utilities.

Django has many utilities : for example html, text,... (linebreaks,
html_count,...).
My projects use Google App engine, so it's very easy to use these
because django is installed by default but if you use Tornado
"standalone" : if you need these, you have to "rewrite" them or add
many libs.

Regards

Alkis Evlogimenos ('Αλκης Ευλογημένος)

unread,
Oct 20, 2009, 12:45:53 PM10/20/09
to python-...@googlegroups.com
+1
--

Alkis

Gabriel Farrell

unread,
Nov 20, 2009, 9:47:55 PM11/20/09
to Tornado Web Server
I know this is an old thread, but if you're just trying to produce a
comma-separated list, you could do

{{', '.join(mylist)}}

or if you want to keep the template squeaky clean pass this function
to the template:

def comma_join(list_):
return ', '.join(list_)

Then your template would use

{{comma_join(mylist)}}
Reply all
Reply to author
Forward
0 new messages