如何在Jinja2中模拟while循环 [英] How to simulate while loop in Jinja2
问题描述
我如何在jinja2中执行以下操作:
How would I do the following in jinja2:
while apples < oranges:
# some work here.
根据 http://jinja.pocoo.org/docs/dev/extensions/#loop-controls ,由于出现的错误,Jinja2不支持while循环.
According to http://jinja.pocoo.org/docs/dev/extensions/#loop-controls, and by the error I am getting, Jinja2 does not support while loops.
问题是,只要apples
的值小于oranges
The question is I want to continuously do some work as long as the value of apples
is less than that of oranges
感谢您的帮助.
也等同于while True:
的东西也很好.
Also something equivalent to while True:
is good also.
推荐答案
要在Jina2中循环,您必须使用:for.要在for块中结束循环,可以使用break. 请参阅: http://jinja.pocoo.org/docs/extensions/#loop-controls .
To loop in Jina2 you have to use : for. To end the loop in the for block you can use break. See : http://jinja.pocoo.org/docs/extensions/#loop-controls.
jinja_env = Environment(extensions=['jinja2.ext.loopcontrols'])
您可以使用以下方法创建无尽"循环:
An "endless" loop you can create with:
{% for _ in range(1, large_number) %}
{% if loop.index > stop_at %}{% break %}{% endif %}
{% endfor %}
这篇关于如何在Jinja2中模拟while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!