Answers.Body: <p>You should be able to access the outer <code>loop.index</code> etc using the <code>loop.parent</code> context. The Twig docs <a href="http://twig.sensiolabs.org/doc/recipes.html#accessing-the-parent-context-in-nested-loops" rel="nofollow">here</a> should give you what you need.</p>
  
  <p>EDIT: You might be getting strange results because the inner loop contains an <code>if</code> so depending on your conditions you might loop over the first item thus making your inner <code>loop.first</code> not work as expected. You can combine <code>for</code> with an <code>if</code> which might get you a bit closer, like this:</p>
  
  <pre><code>{% for thing in things if thing.isAvailable == true %}
  {{-- Your inner stuff --}}
  {% endfor %}
  </code></pre>
  
  <p>More info <a href="http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition" rel="nofollow">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-23T14:04:26.767Z
Answers.LastActivityDate: 2016-03-23T14:11:40.117Z
Answers.LastEditDate: 2016-03-23T14:11:40.117Z
Answers.UserDisplayName: Steve Holland
Body: <p>I'm trying to pull out selected categories in a front-end user profile. The cats have 2 levels and need to be pulled out into a nested list with only the selected options. I've tried different ways of doing this but nothing seems to work by using one for loop. So have nested the second level cats in a second for loop. This will work but the loop.first and loop.last variables are using the parent for loop and not the nested one? Or is there a better/easier way of doing this?</p>
  
  <pre><code>{% for challenge in user.challenges.level(1).options %}
      &lt;li&gt;
          {{ challenge.title }}
          {% for child in user.challenges.level(2).options %}
              {% if child and child.getParent().id == challenge.id %}
                  {% if loop.first %}&lt;ul&gt;{% endif %}
                      &lt;li&gt;{{ child.title }}&lt;/li&gt;
                  {% if loop.last %}&lt;/ul&gt;{% endif %}
              {% endif %}
          {% endfor %}
      &lt;/li&gt;
  {% endfor %}
  </code></pre>
  
Tags: <templating><categories><forloop><category><loop>
Title: Issue with twig loop.first loop.last contained in nested for statement
ThreadText: # Issue with twig loop.first loop.last contained in nested for statement
  
  (Asked by: username_0 on 2016-03-23)
  
  I'm trying to pull out selected categories in a front-end user profile. The cats have 2 levels and need to be pulled out into a nested list with only the selected options. I've tried different ways of doing this but nothing seems to work by using one for loop. So have nested the second level cats in a second for loop. This will work but the loop.first and loop.last variables are using the parent for loop and not the nested one? Or is there a better/easier way of doing this?
  
  ```
  {% for challenge in user.challenges.level(1).options %}
      <li>
          {{ challenge.title }}
          {% for child in user.challenges.level(2).options %}
              {% if child and child.getParent().id == challenge.id %}
                  {% if loop.first %}<ul>{% endif %}
                      <li>{{ child.title }}</li>
                  {% if loop.last %}</ul>{% endif %}
              {% endif %}
          {% endfor %}
      </li>
  {% endfor %}
  
  ```
  
  **Tags:** templating, categories, forloop, category, loop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-23. Score: 1
  
  You should be able to access the outer `loop.index` etc using the `loop.parent` context. The Twig docs [here](http://twig.sensiolabs.org/doc/recipes.html#accessing-the-parent-context-in-nested-loops) should give you what you need.
  
  EDIT: You might be getting strange results because the inner loop contains an `if` so depending on your conditions you might loop over the first item thus making your inner `loop.first` not work as expected. You can combine `for` with an `if` which might get you a bit closer, like this:
  
  ```
  {% for thing in things if thing.isAvailable == true %}
  {{-- Your inner stuff --}}
  {% endfor %}
  
  ```
  
  More info [here](http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition).
  
  
  ---
  
  