Answers.Body: <p>At the moment <code>loop.first</code> and <code>loop.index0</code> are relative to your <code>block.image</code> loop.</p>
  
  <p>If you want them to be set based on your <code>entry.locationSlideshow</code> loop (the parent loop) you can use <code>{{ loop.parent.loop.index0 }}</code> and <code>{{ loop.parent.loop.first }}</code></p>
  
  <p>For more information see the <a href="http://twig.sensiolabs.org/doc/recipes.html#accessing-the-parent-context-in-nested-loops">Twig documentation</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-13T19:56:15.677Z
Answers.Text: Works like a charm. Thanks a lot for this precise explanation!
Answers.UserDisplayName: bennobo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-13T13:11:55.550Z
Answers.LastActivityDate: 2016-06-13T13:11:55.550Z
Answers.UserDisplayName: Luke Pearce
Body: <p>I have a problem with <code>{% if loop.first %}</code> and <code>{{ loop.index0 }}</code> with the following snippet:</p>
  
  <pre><code>{% for block in entry.locationSlideshow %}
    {% switch block.type %}
      {% case 'upload' %}
        {% for image in block.image %}
          &lt;button {% if loop.first %}class="is-active" {% endif %}data-slide="{{ loop.index0 }}"&gt;&lt;span class="show-for-sr"&gt;{{ image.title }}&lt;/span&gt;&lt;/button&gt;
        {% endfor %}
    {% endswitch %} 
  {% endfor %}
  </code></pre>
  
  <p>The problem is that all buttons get the class <em>is-active</em> and <em>0</em> instead of 0, 1, 2, 3...</p>
  
  <p>This is working fine outside of a Matrix field. Any ideas what I am doing wrong here? </p>
  
  <p>Regards<br>
  Ben</p>
  
Tags: <matrix><entry-type>
Title: {% if loop.first %} and {{ loop.index0 }} not working with Matrix
ThreadText: # {% if loop.first %} and {{ loop.index0 }} not working with Matrix
  
  (Asked by: username_0 on 2016-06-13)
  
  I have a problem with `{% if loop.first %}` and `{{ loop.index0 }}` with the following snippet:
  
  ```
  {% for block in entry.locationSlideshow %}
    {% switch block.type %}
      {% case 'upload' %}
        {% for image in block.image %}
          <button {% if loop.first %}class="is-active" {% endif %}data-slide="{{ loop.index0 }}"><span class="show-for-sr">{{ image.title }}</span></button>
        {% endfor %}
    {% endswitch %} 
  {% endfor %}
  
  ```
  
  The problem is that all buttons get the class *is-active* and *0* instead of 0, 1, 2, 3...
  
  This is working fine outside of a Matrix field. Any ideas what I am doing wrong here?
  
  Regards  
  Ben
  
  **Tags:** matrix, entry-type
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-06-13. Score: 8 | Accepted answer
  
  At the moment `loop.first` and `loop.index0` are relative to your `block.image` loop.
  
  If you want them to be set based on your `entry.locationSlideshow` loop (the parent loop) you can use `{{ loop.parent.loop.index0 }}` and `{{ loop.parent.loop.first }}`
  
  For more information see the [Twig documentation](http://twig.sensiolabs.org/doc/recipes.html#accessing-the-parent-context-in-nested-loops).
  
  > **Comments:**
  > **username_0**: Works like a charm. Thanks a lot for this precise explanation!  
  
  ---
  
  