access loop.index when within another loop in twig
Solution 1:
In fact there's no need to set an extra variable. For two nested loops twig provides the so called parent.loop
context.
To access the parents loop.index
do this:
{% for i in range(0, 3) %}
{% for j in range(0, 9) %}
{{ loop.parent.loop.index + loop.index }}
{% endfor %}
{% endfor %}
Also refer to the documentation
Solution 2:
set a variable which hold the first loop.index
{% for i in range(0, 3) %}
{% set loop1 = loop.index %}
{% for j in range(0, 9) %}
{{ loop1 + loop.index }}
{% endfor %}
{% endfor %}