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

CC BY-SA 3.0
2014-09-17T18:45:51.023Z
Hi Brad, that was exactly the issue, thank you.
Gareth
CC BY-SA 3.0
2014-09-17T18:18:52.607Z
2014-09-17T18:18:52.607Z
Brad Bell
<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>

CC BY-SA 3.0
2014-09-17T08:30:07.750Z
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?
André Elvan
CC BY-SA 3.0
2014-09-17T08:34:40.917Z
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?
Gareth
CC BY-SA 3.0
2014-09-17T09:35:17.540Z
If you try to just output {{ loop.index0 }} outside of the switch, does that work? Try removing everything else inside the for loop.
André Elvan
CC BY-SA 3.0
2014-09-17T09:39:46.277Z
Yeah that works, its just when you put the `{{ loop.index0 }}` inside of the switch.
Gareth
CC BY-SA 3.0
2014-09-17T09:58:28.113Z
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? :)
André Elvan
<matrix>
How do I cycle a class on a Matrix block?
# 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' %}
            
                ...
            

        {% 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' %}
            
                ...
            

        {% case 'imageBlock' %}
            ...

    {% endswitch %}

{% endfor %}

> **Comments:**
> **username_0**: Hi Brad, that was exactly the issue, thank you.  

---

