loop.index is not returning correct number inside nested loop with inline if conditional I'm getting incorrect loop.index results with the following code. It returns "1" for each entry returned. Is there a different way this needs to be written?

{% for order in orders %}
    {% for refund in order.transactions if refund.type == "refund" %}
        {{ loop.index  }}
    {% endfor %}
{% endfor %}
I'm guessing you want to display the loop index of the outer for loop.

To do that, Twig provides a loop.parent context.

{% for order in orders %}
    {% for refund in order.transactions if refund.type == "refund" %}
        {{ loop.parent.loop.index  }}
    {% endfor %}
{% endfor %}


If you're looking to get the total number of times the inner loop gets hit, then something like this should work:

{% set loopCount = 0 %}

{% for order in orders %}
    {% for refund in order.transactions if refund.type == "refund" %}
        {% set loopCount = loopCount + 1 %}
        {{ loopCount  }}
    {% endfor %}
{% endfor %}