Message from discussion
jinja - in lieu of enumerate and list comprehension
Date: Mon, 6 Aug 2012 07:27:20 -0700 (PDT)
From: Andreas <achristoffer...@gmail.com>
To: pocoo-libs@googlegroups.com
Message-Id: <3fe6d54a-f0a6-4750-9b5e-2ed1a3040a71@googlegroups.com>
Subject: jinja - in lieu of enumerate and list comprehension
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_333_26987868.1344263240212"
------=_Part_333_26987868.1344263240212
Content-Type: multipart/alternative;
boundary="----=_Part_334_29840868.1344263240212"
------=_Part_334_29840868.1344263240212
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Hi - first time with Jinja, and not very clever I am sure.
I need to pass something like "1,2,3" to the jquery sparkline plugin.
I have a list = [1,2,3].
First I tried list comprehension:
{{ ', '.join([str(x) for x in list]) }}
But jinja does not support this. Then I tried enumerate:
{% for p, i in enumerate(list) %}
{% if p == 0 %}
{{ i }}
{% else %}
,{{ i }}
{% endif %}
{% endfor %}
But jinja does not like enumerate either? There must be a smart way of
doing this?
Thanks in advance.
------=_Part_334_29840868.1344263240212
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Hi - first time with Jinja, and not very clever I am sure.<div><br></div><d=
iv>I need to pass something like "1,2,3" to the jquery sparkline plugin.</d=
iv><div><br></div><div>I have a list =3D [1,2,3].</div><div><br></div><div>=
First I tried list comprehension:</div><blockquote style=3D"margin: 0 0 0 4=
0px; border: none; padding: 0px;"><div> {{ ', '.join([str(x) for x in =
list]) }}</div></blockquote><div><br></div><div>But jinja does not support =
this. Then I tried enumerate:</div><div><br></div><blockquote style=3D"marg=
in: 0 0 0 40px; border: none; padding: 0px;"><div>{% for p, i in enumerate(=
list) %}</div><div>{% if p =3D=3D 0 %}</div><div>{{ i }}</div><div>{% else =
%}</div><div>,{{ i }}</div><div>{% endif %}</div><div>{% endfor %}</div></b=
lockquote><div><br></div><div> But jinja does not like enumerate eithe=
r? There must be a smart way of doing this?</div><div><br></div><div>Thanks=
in advance.</div><div> </div><blockquote style=3D"margin: 0 0 0 40px;=
border: none; padding: 0px;"><div> </div></blockquote>
------=_Part_334_29840868.1344263240212--
------=_Part_333_26987868.1344263240212--