Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate while loop in Jinja2

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.

like image 692
kasavbere Avatar asked Dec 15 '12 23:12

kasavbere


1 Answers

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 %}
like image 101
voscausa Avatar answered Nov 15 '22 08:11

voscausa