Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T01:18:23.443Z
Answers.Text: 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  }}
Answers.UserDisplayName: Anna_MediaGirl
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T01:49:43.657Z
Answers.Text: 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.
Answers.UserDisplayName: Anna_MediaGirl
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T20:34:37.603Z
Answers.Text: Hrm... check my edit and see if that's what you're looking for.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-16T01:45:14.623Z
Answers.Text: That did the trick!
Answers.UserDisplayName: Anna_MediaGirl
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-14T21:47:14.977Z
Answers.LastActivityDate: 2017-11-15T20:34:10.057Z
Answers.LastEditDate: 2017-11-15T20:34:10.057Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-14T21:45:13.260Z
Comments.Text: 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.
Comments.UserDisplayName: Brad Bell
Tags: <forloop><count>
Title: loop.index is not returning correct number inside nested loop with inline if conditional
ThreadText: # 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`](https://twig.symfony.com/doc/2.x/recipes.html#accessing-the-parent-context-in-nested-loops) 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!  
  
  ---
  
  