<p>The <code>loop.index0</code> variable will be incremented for each block, regardless of type, so assuming every other block is a <code>header</code>, it's not surprising that the <code>cycle</code> method prints <code>"even"</code> for every <code>text</code> block.</p>

<p>I think what you need is a different variable which only increments whenever you hit a <code>text</code> block (note that this variable should be declared outside the <code>for</code> loop):</p>

<pre><code>{% set textIndex = 0 %}

{% for block in entry.articlebody %}

    {% switch block.type %}

        {% case 'heading' %}
            &lt;h2 class="section-title"&gt;{{ block.heading }}&lt;/h2&gt;&lt;!-- /.section-title --&gt;

        {% case 'text' %}
            &lt;div class="{{ cycle(['odd', 'even'], textIndex) }}"&gt;{{ block.text }}&lt;/div&gt;
            {% set textIndex = textIndex + 1 %}

    {% endswitch %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-11-08T00:57:40.663Z
Works like a charm, Mats! Seems like I need to dig deeper into the Twig docs:-)
Nutmeg
CC BY-SA 3.0
2016-11-07T23:04:57.937Z
2016-11-07T23:04:57.937Z
Mats Mikkel Rummelhoff
<p>I think I am doing exactly the same as <a href="https://craftcms.stackexchange.com/questions/2582/how-do-i-cycle-a-class-on-a-matrix-block">here</a> but I only get the "even" class at each iteration.</p>

<pre><code>{% for block in entry.articlebody %}

    {% set index0 = loop.index0 %}

    {% switch block.type %}

        {% case 'heading' %}
            &lt;h2 class="section-title"&gt;{{ block.heading }}&lt;/h2&gt;&lt;!-- /.section-title --&gt;

        {% case 'text' %}
            &lt;div class="{{ cycle(['odd', 'even'], index0) }}"&gt;{{ block.text }}&lt;/div&gt;    

    {% endswitch %}
{% endfor %}
</code></pre>

<matrix><classes>
How do I (also) cycle a class on a Matrix block?
# How do I (also) cycle a class on a Matrix block?

(Asked by: username_0 on 2016-11-07)

I think I am doing exactly the same as here but I only get the "even" class at each iteration.
{% for block in entry.articlebody %}

    {% set index0 = loop.index0 %}

    {% switch block.type %}

        {% case 'heading' %}
            {{ block.heading }}

        {% case 'text' %}
            {{ block.text }}    

    {% endswitch %}
{% endfor %}

**Tags:** matrix, classes

---

## 1 answers

### Answer by username_1 on 2016-11-07. Score: 2 | Accepted answer

The loop.index0 variable will be incremented for each block, regardless of type, so assuming every other block is a header, it's not surprising that the cycle method prints "even" for every text block.
I think what you need is a different variable which only increments whenever you hit a text block (note that this variable should be declared outside the for loop):
{% set textIndex = 0 %}

{% for block in entry.articlebody %}

    {% switch block.type %}

        {% case 'heading' %}
            {{ block.heading }}

        {% case 'text' %}
            {{ block.text }}
            {% set textIndex = textIndex + 1 %}

    {% endswitch %}
{% endfor %}

> **Comments:**
> **username_0**: Works like a charm, Mats! Seems like I need to dig deeper into the Twig docs:-)  

---

