Message from discussion
jinja - in lieu of enumerate and list comprehension
Received: by 10.180.106.199 with SMTP id gw7mr1206410wib.0.1344265058238;
Mon, 06 Aug 2012 07:57:38 -0700 (PDT)
X-BeenThere: pocoo-libs@googlegroups.com
Received: by 10.180.96.67 with SMTP id dq3ls3008076wib.1.gmail; Mon, 06 Aug
2012 07:57:37 -0700 (PDT)
Received: by 10.180.24.202 with SMTP id w10mr1298130wif.0.1344265057648;
Mon, 06 Aug 2012 07:57:37 -0700 (PDT)
Received: by 10.180.24.202 with SMTP id w10mr1298129wif.0.1344265057638;
Mon, 06 Aug 2012 07:57:37 -0700 (PDT)
Return-Path: <skryska...@gmail.com>
Received: from mail-wi0-f176.google.com (mail-wi0-f176.google.com [209.85.212.176])
by gmr-mx.google.com with ESMTPS id e5si1866398wiw.0.2012.08.06.07.57.37
(version=TLSv1/SSLv3 cipher=OTHER);
Mon, 06 Aug 2012 07:57:37 -0700 (PDT)
Received-SPF: pass (google.com: domain of skryska...@gmail.com designates 209.85.212.176 as permitted sender) client-ip=209.85.212.176;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of skryska...@gmail.com designates 209.85.212.176 as permitted sender) smtp.mail=skryska...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-wi0-f176.google.com with SMTP id hn17so1470553wib.11
for <pocoo-libs@googlegroups.com>; Mon, 06 Aug 2012 07:57:37 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:from:date:message-id:subject:to
:content-type;
bh=vhPRT4Ecn5HN0MWCAcNz+2MQ0NUsVQp36W0M1tN6FvE=;
b=mfVGYKwtAA4Klx1ixopL2BQ+br3oLhQqTwL0CBKCGTeg2OoL10GGbu3wV7gwo6wXPl
VXutkAfZRgFuPA7eSZ2OeoPkgUO6TBPvr1WWBgJ4tBKIH2+WtqSwIsIk2GeiDQwYcuWc
ATtxLExlIo6t5S0zpaetIAP1WJN4rmMHt1zuFFaMMUi+Ybx4Aj71c7tdI2Gv5OPYD+Cw
yGk0H3SzWfYa0U9WQGMQpIdbmhfX4cdiNdHxn8STHFEm9j1wpeXeU9VaNwyZ/nUpg55y
nfAvRcMsxjdnJcwgdfwv7LmJigepRRmlNCv9qTL6yC1rMBQKPALB8LH5Ytl/K3uRLrDk
xZEg==
Received: by 10.180.100.131 with SMTP id ey3mr19127996wib.15.1344265057538;
Mon, 06 Aug 2012 07:57:37 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.216.208.158 with HTTP; Mon, 6 Aug 2012 07:56:57 -0700 (PDT)
In-Reply-To: <3fe6d54a-f0a6-4750-9b5e-2ed1a3040a71@googlegroups.com>
References: <3fe6d54a-f0a6-4750-9b5e-2ed1a3040a71@googlegroups.com>
From: Steven Kryskalla <skryska...@gmail.com>
Date: Mon, 6 Aug 2012 07:56:57 -0700
Message-ID: <CADymufaphBKN9t0zb8-xJ-pOf6yYiZrvmt6bPdo8rE5P1g_...@mail.gmail.com>
Subject: Re: jinja - in lieu of enumerate and list comprehension
To: pocoo-libs@googlegroups.com
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Aug 6, 2012 at 7:27 AM, Andreas <achristoffer...@gmail.com> wrote:
> But jinja does not like enumerate either? There must be a smart way of
> doing this?
Jinja2 has special "loop" variables
(http://jinja.pocoo.org/docs/templates/#for), for example loop.index
to get the current iteration of the loop, and loop.last to tell if
this is the last iteration of the loop. So, you can do this:
>>> jinja2.Template("{% for x in xs %}{{x}}{% if not loop.last %},{% endif %}{% endfor %}").render(xs=[10,20,30,40])
u'10,20,30,40'
Or, if you wanted to use enumerate, you can pass it into the template:
>>> jinja2.Template("{% for i,x in enumerate(xs) %}{{ (i,x) }} {% endfor %}").render(xs=[10,20,30,40], enumerate=enumerate)
u'(0, 10) (1, 20) (2, 30) (3, 40) '
Or you could just write a function that does what you need, and pass
that function into the template:
>>> join_with_commas = lambda xs: ", ".join(str(x) for x in xs)
>>> jinja2.Template("{{ join_with_commas(xs) }}").render(xs=[10,20,30,40], join_with_commas=join_with_commas)
u'10, 20, 30, 40'
And finally, the best solution would be to just use the builtin "join" filter!
>>> jinja2.Template("{{ xs | join(', ') }}").render(xs=[10,20,30,40])
u'10, 20, 30, 40'
http://jinja.pocoo.org/docs/templates/#join
-steve