Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-08T00:57:40.663Z
Answers.Text: Works like a charm, Mats! Seems like I need to dig deeper into the Twig docs:-)
Answers.UserDisplayName: Nutmeg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-07T23:04:57.937Z
Answers.LastActivityDate: 2016-11-07T23:04:57.937Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <matrix><classes>
Title: How do I (also) cycle a class on a Matrix block?
ThreadText: # 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](https://craftcms.stackexchange.com/questions/2582/how-do-i-cycle-a-class-on-a-matrix-block) but I only get the "even" class at each iteration.
  
  ```
  {% for block in entry.articlebody %}
  
      {% set index0 = loop.index0 %}
  
      {% switch block.type %}
  
          {% case 'heading' %}
              <h2 class="section-title">{{ block.heading }}</h2><!-- /.section-title -->
  
          {% case 'text' %}
              <div class="{{ cycle(['odd', 'even'], index0) }}">{{ block.text }}</div>    
  
      {% 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' %}
              <h2 class="section-title">{{ block.heading }}</h2><!-- /.section-title -->
  
          {% case 'text' %}
              <div class="{{ cycle(['odd', 'even'], textIndex) }}">{{ block.text }}</div>
              {% set textIndex = textIndex + 1 %}
  
      {% endswitch %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Works like a charm, username_1! Seems like I need to dig deeper into the Twig docs:-)  
  
  ---
  
  