Nunjucks: Loop Controls

Created on 15 Oct 2014  路  22Comments  路  Source: mozilla/nunjucks

There is no possibility to stop the cycle, such as:

{% for user in users %}
    {%- if loop.index >= 10 %}{% break %}{% endif %}
{%- endfor %}

it returns an error:
unknown block tag: break

enhancement

Most helpful comment

Next/Continue would also be nice. Currently same thing can be implemented with IF conditions, but this would produce more readable code.

All 22 comments

Jinja doesn't support this either (which is why it probably won't be implemented in Nunjucks). See http://jinja.pocoo.org/docs/dev/templates/#for for suggestions to avoid it.

I see that it is implemented in Jinja)
http://jinja.pocoo.org/docs/dev/templates/#loop-controls

Sorry, I was reading an old version of the documentation. You're entirely correct.

it would be really nice to see break tag since it the only way to implement while loops in Jinja and nunjucks

:+1:

Just stumbled across this looking for the same feature. This would be a welcome feature.

Agreed, this would be a useful feature!

yep...
:+1:

:+1:

:+1: definitely useful feature

I think this is not an enhancement but a bug.

:+1:

+1

+1

+1

+1

Next/Continue would also be nice. Currently same thing can be implemented with IF conditions, but this would produce more readable code.

A year later, still would love a break :)

I also would love to see this.

Break and continue has been implemented as an extension called loopcontrols:

I added this line on my flask app and it worked fine !

app.jinja_env.add_extension('jinja2.ext.loopcontrols')

Hi guys, any idea how i can actually implement loopextensions?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thisKai picture thisKai  路  3Comments

mintyPT picture mintyPT  路  4Comments

ziir picture ziir  路  3Comments

kud picture kud  路  3Comments

dgirgenti picture dgirgenti  路  3Comments