python - Get the current and next value in a Jinja for loop -


is there way iterate on list in jinja2 can access nth , (n+1) th element in single iteration?

{% x in my_list %}  <!-- print current , (current+1) --> {{x}}, {{x+1}} <-- wrong highlights problem  {% endfor %} 

also, loop allow "step" increment?

jinja has loop.index variable can use access next value in list:

@app.route('/') def index():     return render_template_string("""         <body>             {% x in my_list %}                 <p>                     {{ x }} {{ my_list[loop.index] }}<br>                 </p>             {% endfor %}         </body>     """, my_list=range(10)) 

0 1

1 2

2 3

3 4

4 5

5 6

6 7

7 8

8 9

9


Comments