Answers.Body: <p>This is mostly likely a scoping issue inside the <code>switch</code> statement.  Try something like this:</p>
  
  <pre><code>{% for block in entry.asideNavigation %}
  
      {% set index0 = loop.index0 %}
  
      {% switch block.type %}
  
          {% case 'textBlock' %}
              &lt;li class="aside__nav__item {{ cycle(['cs-a','cs-b'], index0) }}"&gt;
                  ...
              &lt;/li&gt;
  
          {% case 'imageBlock' %}
              ...
  
      {% endswitch %}
  
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T18:45:51.023Z
Answers.Text: Hi Brad, that was exactly the issue, thank you.
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T18:18:52.607Z
Answers.LastActivityDate: 2014-09-17T18:18:52.607Z
Answers.UserDisplayName: Brad Bell
Body: <p>I am trying to add a class which will cycle through on every iteration of a matrix block type, here is my code:</p>
  
  <pre><code>{% for block in entry.asideNavigation %}
  
      {% switch block.type %}
  
          {% case 'textBlock' %}
              &lt;li class="aside__nav__item {{ cycle(['cs-a','cs-b'], loop.index0) }}"&gt;
                  ...
              &lt;/li&gt;
  
          {% case 'imageBlock' %}
              ...
  
      {% endswitch %}
  
  {% endfor %}
  </code></pre>
  
  <p>This cycle function gives me the error "Variable "loop" does not exist" where am I going wrong?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T08:30:07.750Z
Comments.Text: That is weird, the code works fine for me. Have you tried isolating only this code? Maybe there's something wrong in the rest of your twig code that triggers this?
Comments.UserDisplayName: André Elvan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T08:34:40.917Z
Comments.Text: Weird, I thought it should work. Yes everything works fine if I remove the cycle function so it must be something to do with the loop in matrix field?
Comments.UserDisplayName: Gareth
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T09:35:17.540Z
Comments.Text: If you try to just output {{ loop.index0 }} outside of the switch, does that work? Try removing everything else inside the for loop.
Comments.UserDisplayName: André Elvan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T09:39:46.277Z
Comments.Text: Yeah that works, its just when you put the `{{ loop.index0 }}` inside of the switch.
Comments.UserDisplayName: Gareth
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T09:58:28.113Z
Comments.Text: It works inside a switch in my tests. And with the exact same cycle that you have. You sure there isn't some other twig code in the ... that messes with you? :)
Comments.UserDisplayName: André Elvan
Tags: <matrix>
Title: How do I cycle a class on a Matrix block?
ThreadText: # How do I cycle a class on a Matrix block?
  
  (Asked by: username_0 on 2014-09-17)
  
  I am trying to add a class which will cycle through on every iteration of a matrix block type, here is my code:
  
  ```
  {% for block in entry.asideNavigation %}
  
      {% switch block.type %}
  
          {% case 'textBlock' %}
              <li class="aside__nav__item {{ cycle(['cs-a','cs-b'], loop.index0) }}">
                  ...
              </li>
  
          {% case 'imageBlock' %}
              ...
  
      {% endswitch %}
  
  {% endfor %}
  
  ```
  
  This cycle function gives me the error "Variable "loop" does not exist" where am I going wrong?
  
  **Tags:** matrix
  
  > **Comments:**
  > **username_1**: That is weird, the code works fine for me. Have you tried isolating only this code? Maybe there's something wrong in the rest of your twig code that triggers this?  
  > **username_0**: Weird, I thought it should work. Yes everything works fine if I remove the cycle function so it must be something to do with the loop in matrix field?  
  > **username_1**: If you try to just output {{ loop.index0 }} outside of the switch, does that work? Try removing everything else inside the for loop.  
  > **username_0**: Yeah that works, its just when you put the `{{ loop.index0 }}` inside of the switch.  
  > **username_1**: It works inside a switch in my tests. And with the exact same cycle that you have. You sure there isn't some other twig code in the ... that messes with you? :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2014-09-17. Score: 3 | Accepted answer
  
  This is mostly likely a scoping issue inside the `switch` statement. Try something like this:
  
  ```
  {% for block in entry.asideNavigation %}
  
      {% set index0 = loop.index0 %}
  
      {% switch block.type %}
  
          {% case 'textBlock' %}
              <li class="aside__nav__item {{ cycle(['cs-a','cs-b'], index0) }}">
                  ...
              </li>
  
          {% case 'imageBlock' %}
              ...
  
      {% endswitch %}
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Hi username_2, that was exactly the issue, thank you.  
  
  ---
  
  