<p>I'm guessing you want to display the loop index of the outer for loop.</p>

<p>To do that, Twig provides a <a href="https://twig.symfony.com/doc/2.x/recipes.html#accessing-the-parent-context-in-nested-loops" rel="nofollow noreferrer"><code>loop.parent</code></a> context.</p>

<pre><code>{% for order in orders %}
    {% for refund in order.transactions if refund.type == "refund" %}
        {{ loop.parent.loop.index  }}
    {% endfor %}
{% endfor %}
</code></pre>

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

<pre><code>{% set loopCount = 0 %}

{% for order in orders %}
    {% for refund in order.transactions if refund.type == "refund" %}
        {% set loopCount = loopCount + 1 %}
        {{ loopCount  }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-11-15T01:18:23.443Z
I was looking for the inner loop count. I see your comment above and I think I understand what's happening now... What I'm looking to do is number a table column from 1...XXX. Let me try {{ loop.parent.loop.index  }}
Anna_MediaGirl
CC BY-SA 3.0
2017-11-15T01:49:43.657Z
Negative... Using {{ loop.parent.loop.index  }} didn't get me the count numbers either... I'll see if I can return a row number another way.
Anna_MediaGirl
CC BY-SA 3.0
2017-11-15T20:34:37.603Z
Hrm... check my edit and see if that's what you're looking for.
Brad Bell
CC BY-SA 3.0
2017-11-16T01:45:14.623Z
That did the trick!
Anna_MediaGirl
CC BY-SA 3.0
2017-11-14T21:47:14.977Z
2017-11-15T20:34:10.057Z
2017-11-15T20:34:10.057Z
Brad Bell
<p>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?</p>

<pre><code>{% for order in orders %}
    {% for refund in order.transactions if refund.type == "refund" %}
        {{ loop.index  }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-11-14T21:45:13.260Z
You want the loop index of the inner for loop or do you want the loop index of the outer?  If each order only has 1 transaction, I'd expect your code to output `1`, `orders` number of times.
Brad Bell
<forloop><count>
loop.index is not returning correct number inside nested loop with inline if conditional
# loop.index is not returning correct number inside nested loop with inline if conditional

(Asked by: username_0 on 2017-11-14)

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 %}

**Tags:** forloop, count

> **Comments:**
> **username_1**: You want the loop index of the inner for loop or do you want the loop index of the outer?  If each order only has 1 transaction, I'd expect your code to output `1`, `orders` number of times.  

---

## 1 answers

### Answer by username_1 on 2017-11-14. Score: 3 | Accepted answer

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 %}

> **Comments:**
> **username_0**: I was looking for the inner loop count. I see your comment above and I think I understand what's happening now... What I'm looking to do is number a table column from 1...XXX. Let me try {{ loop.parent.loop.index  }}  
> **username_0**: Negative... Using {{ loop.parent.loop.index  }} didn't get me the count numbers either... I'll see if I can return a row number another way.  
> **username_1**: Hrm... check my edit and see if that's what you're looking for.  
> **username_0**: That did the trick!  

---

