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
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 :)
Would also love to see continue implemented!
http://jinja.pocoo.org/docs/2.10/extensions/#loop-controls
http://jinja.pocoo.org/docs/2.10/templates/#loop-controls
https://github.com/pallets/jinja/blob/7a6704db55fcd9bbe5a90c3868e03f5a5fcf176a/jinja2/ext.py#L418-L426
https://github.com/pallets/jinja/blob/7a6704db55fcd9bbe5a90c3868e03f5a5fcf176a/jinja2/nodes.py#L948-L953
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?
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.