Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Jinja2: default text when iterating over list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marten Lehmann  
View profile  
 More options Sep 30 2012, 11:38 pm
From: Marten Lehmann <lehm...@cnm.de>
Date: Mon, 01 Oct 2012 05:38:49 +0200
Local: Sun, Sep 30 2012 11:38 pm
Subject: Jinja2: default text when iterating over list
Hello,

while I'm working with Jinja2 I noticed, that Twig (a PHP templating
engine) uses virtually the same template syntax, but additionally offers
a handy feature in case a list of a for-loop doesn't contain any entries:

{% for user in users %}
     * {{ user.name }}
{% else %}
     No user have been found.
{% endfor %}

How is it supposed to be done in Jinja2? At least the documentation
doesn't mention an else-directive for the for-loop. An outer
if-condition would probably work, but wouldn't look very elegant.

Kind regards
Marten


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Kryskalla  
View profile  
 More options Sep 30 2012, 11:44 pm
From: Steven Kryskalla <skryska...@gmail.com>
Date: Sun, 30 Sep 2012 20:43:51 -0700
Local: Sun, Sep 30 2012 11:43 pm
Subject: Re: Jinja2: default text when iterating over list

On Sun, Sep 30, 2012 at 8:38 PM, Marten Lehmann <lehm...@cnm.de> wrote:
> How is it supposed to be done in Jinja2? At least the documentation doesn't
> mention an else-directive for the for-loop. An outer if-condition would
> probably work, but wouldn't look very elegant.

It is mentioned in the docs here:

http://jinja.pocoo.org/docs/templates/#for

"If no iteration took place because the sequence was empty or the
filtering removed all the items from the sequence you can render a
replacement block by using else"

This is working for me:

>>> t = jinja2.Template("{% for x in xs %}* {{ x }}\n{% else %}Nothing found.{% endfor %}")
>>> t.render(xs=[1,2,3])
u'* 1\n* 2\n* 3\n'
>>> t.render(xs=[])

u'Nothing found.'

-Steve


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marten Lehmann  
View profile  
 More options Oct 1 2012, 12:29 pm
From: Marten Lehmann <lehm...@cnm.de>
Date: Mon, 1 Oct 2012 09:29:13 -0700 (PDT)
Local: Mon, Oct 1 2012 12:29 pm
Subject: Re: Jinja2: default text when iterating over list

On Monday, October 1, 2012 5:44:35 AM UTC+2, Steven Kryskalla wrote:

> It is mentioned in the docs here:

> http://jinja.pocoo.org/docs/templates/#for

> "If no iteration took place because the sequence was empty or the
> filtering removed all the items from the sequence you can render a
> replacement block by using else"

Thanks, you're right. I obviously wasn't reading the explicit
for-documentation, but I was just looking at arbitrary examples of the for
loop everywhere else, which don't contain the else part.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »