Minor doc fixes. #69

wants to merge 1 commit into


None yet
3 participants

Cito commented Oct 23, 2011

No description provided.


rduplain commented Mar 9, 2012

Note that the else part works differently in ordinary Python where it
is executed when a break happened, which is not possible here.

@mitsuhiko Can you confirm this? If true, will pull.


njl commented Mar 12, 2012

This is not the case.


"Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement."

@rduplain rduplain closed this Mar 13, 2012


rduplain commented Mar 13, 2012

I pulled in the typo fixes, now a21f2ed. Thanks.


Cito commented Mar 21, 2012

Sorry, in that note above I wanted to say "where it is executed when a break did not happen", not "where it is executed when a break happened", so it is good that you didn't pull it.

But the main point here that the for statement behaves differently from ordinary Python is still correct and should still be mentioned. In ordinary Python, the else part would always be executed when there is no break, while in Jinja it is only executed when no iteration took place. Since in Jinja breaks are not possible, the else part would be always executed, so it makes sense that the behavior was changed. However, readers should be made aware of that different behavior.


rduplain commented Mar 21, 2012

Thanks for following up. The documentation currently reads:

"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:"

Is this not enough?


Cito commented Mar 21, 2012

There's nothing wrong with the documentation.

I just think it deserves special mention when the behavior is different from ordinary Python, because you usually expect Jinja2 keywords to operate in the same way as the corresponding Python keywords. However, this is not the case for else. For instance, the Jinja2 template

{% for i in range(3) %} {{i}} {% else %} nothing {% endfor %}

will just output "0 1 2" while the Python code

for i in range (3): print i,
else: print "nothing"

will output "0 1 2 nothing".


rduplain commented Mar 21, 2012

I'll find a place for this, unless you'd like to submit another pull request.


Cito commented Mar 21, 2012

Ok, this is now pull request #112.

mitsuhiko added a commit that referenced this pull request May 19, 2013

Merge pull request #112 from Cito/master
Addition/correction for pull request #69 - add note to docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment