Answers.Body: <p>Building on Syversen's answer of how to achieve this, I'd make that text block a partial template so it's only written once and do the following:</p>
  
  <pre><code>{% case "splitImageAndText" %}
      &lt;div class="grid-x align-center paddingBottom"&gt;
          {% set images = block.imagePart %}
  
          {% if loop.index is even %}
              {% include 'partials/text-block' %}
          {% endif %}
  
          {% if images and images|length %}
              {% set image = block.imagePart.all() %}
  
              {# Loop through the images #}
              {% for image in images.all() %}
                  &lt;div class="cell medium-6 small-12"&gt;
                      &lt;div class="bg-content" style="background-image:url('{{ image.getUrl() }}');"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              {% endfor %}
          {% endif %}
  
          {% if loop.index is odd %}
              {% include 'partials/text-block' %}
          {% endif %}
      &lt;/div&gt;
  </code></pre>
  
  <p>This way, if you ever need to alter the text block markup, it's only in one location instead of having any repeated markup. So in <code>partials/text-block.twig</code>, I'd have:</p>
  
  <pre><code>&lt;div class="cell medium-6 small-12 pull-left"&gt;
      {{ block.textPart }}
  &lt;/div&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-20T17:34:19.970Z
Answers.Text: And thank you Jay, that is very sensible, thank you too.
Answers.UserDisplayName: moondawg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-20T12:55:45.283Z
Answers.LastActivityDate: 2018-08-20T12:55:45.283Z
Answers.UserDisplayName: Jay
Answers.Body: <p>I belive you can use {% if loop.index is even %} or {% if loop.index is odd %}</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-20T17:33:55.073Z
Answers.Text: Ah yes, makes sense now, perfect, thank you.
Answers.UserDisplayName: moondawg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-20T12:21:56.243Z
Answers.LastActivityDate: 2018-08-20T12:21:56.243Z
Answers.UserDisplayName: Syversen
Body: <p>How would one construct this? I have built a little content builder and wish now to change the layout HTML on the fly when there are more than one matrix block.</p>
  
  <p>In the example below, this is just one section of the builder and the one I would like to change.</p>
  
  <p>Basically 'move' the block.text part above the image every 2nd time in the loop. I have looked at batch and loopindex but having trouble for the last few hours. Can someone please help with this?</p>
  
  <p>Builder code:</p>
  
  <pre><code>{% case "splitImageAndText" %}
      &lt;div class="grid-x align-center paddingBottom"&gt;
          {% set images = block.imagePart %}
  
          {% if images and images|length %}
              {% set image = block.imagePart.all() %}
  
              {# Loop through the images #}
              {% for image in images.all() %}
                  &lt;div class="cell medium-6 small-12"&gt;
                      &lt;div class="bg-content" style="background-image:url('{{ image.getUrl() }}');"&gt;&lt;/div&gt;
                  &lt;/div&gt;
              {% endfor %}
          {% endif %}
  
          &lt;div class="cell medium-6 small-12 pull-left"&gt;
              {{ block.textPart }}
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
Tags: <matrix><forloop><loop><case>
Title: Loop index inside a switch content builder
ThreadText: # Loop index inside a switch content builder
  
  (Asked by: username_0 on 2018-08-20)
  
  How would one construct this? I have built a little content builder and wish now to change the layout HTML on the fly when there are more than one matrix block.
  
  In the example below, this is just one section of the builder and the one I would like to change.
  
  Basically 'move' the block.text part above the image every 2nd time in the loop. I have looked at batch and loopindex but having trouble for the last few hours. Can someone please help with this?
  
  Builder code:
  
  ```
  {% case "splitImageAndText" %}
      <div class="grid-x align-center paddingBottom">
          {% set images = block.imagePart %}
  
          {% if images and images|length %}
              {% set image = block.imagePart.all() %}
  
              {# Loop through the images #}
              {% for image in images.all() %}
                  <div class="cell medium-6 small-12">
                      <div class="bg-content" style="background-image:url('{{ image.getUrl() }}');"></div>
                  </div>
              {% endfor %}
          {% endif %}
  
          <div class="cell medium-6 small-12 pull-left">
              {{ block.textPart }}
          </div>
      </div>
  
  ```
  
  **Tags:** matrix, forloop, loop, case
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-08-20. Score: 1 | Accepted answer
  
  Building on username_2's answer of how to achieve this, I'd make that text block a partial template so it's only written once and do the following:
  
  ```
  {% case "splitImageAndText" %}
      <div class="grid-x align-center paddingBottom">
          {% set images = block.imagePart %}
  
          {% if loop.index is even %}
              {% include 'partials/text-block' %}
          {% endif %}
  
          {% if images and images|length %}
              {% set image = block.imagePart.all() %}
  
              {# Loop through the images #}
              {% for image in images.all() %}
                  <div class="cell medium-6 small-12">
                      <div class="bg-content" style="background-image:url('{{ image.getUrl() }}');"></div>
                  </div>
              {% endfor %}
          {% endif %}
  
          {% if loop.index is odd %}
              {% include 'partials/text-block' %}
          {% endif %}
      </div>
  
  ```
  
  This way, if you ever need to alter the text block markup, it's only in one location instead of having any repeated markup. So in `partials/text-block.twig`, I'd have:
  
  ```
  <div class="cell medium-6 small-12 pull-left">
      {{ block.textPart }}
  </div>
  
  ```
  
  > **Comments:**
  > **username_0**: And thank you Jay, that is very sensible, thank you too.  
  
  ---
  
  ### Answer by username_2 on 2018-08-20. Score: 1
  
  I belive you can use {% if loop.index is even %} or {% if loop.index is odd %}
  
  > **Comments:**
  > **username_0**: Ah yes, makes sense now, perfect, thank you.  
  
  ---
  
  