How to simulate while loop in Jinja2

半城伤御伤魂 提交于 2019-12-07 04:35:52

问题


How would I do the following in jinja2:

while apples < oranges:
    # some work here.

According to http://jinja.pocoo.org/docs/dev/extensions/#loop-controls, and by the error I am getting, Jinja2 does not support while loops.

The question is I want to continuously do some work as long as the value of apples is less than that of oranges

Thanks for any help.

Also something equivalent to while True: is good also.


回答1:


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 %}


来源:https://stackoverflow.com/questions/13897001/how-to-simulate-while-loop-in-jinja2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!