I'm working on a project in flask and am trying a trick to run two foreach loops. I'm doing this by creating a variable and adding 1 to it after each loop like so:
{% set iteration = 0 %}
{% for result in results %}
<p id="{{ results_raw[iteration] }}">{{ result }}</p> <br>
{% set iteration = iteration 1 %}
{% endfor %}
The iteration variable should go from 0 to 1 to 2 and so on. "results_raw" is a dictionary with 6 strings. When this code runs, the 0th string keeps printing over and over, meaning that the iterations variable is not increasing. What could be the cause of this?
CodePudding user response:
Instead of setting the iteration variable, you could access loop.index and loop.index0 inside a for loop
Refer to this documentation: https://jinja.palletsprojects.com/en/3.1.x/templates/
loop.index , The current iteration of the loop. (1 indexed)
loop.index0, The current iteration of the loop. (0 indexed)
{% for result in results %}
<p id="{{ results_raw[loop.index0] }}">{{ result }}</p> <br>
{% endfor %}