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

CC BY-SA 4.0
2018-08-20T17:34:19.970Z
And thank you Jay, that is very sensible, thank you too.
moondawg
CC BY-SA 4.0
2018-08-20T12:55:45.283Z
2018-08-20T12:55:45.283Z
Jay
<p>I belive you can use {% if loop.index is even %} or {% if loop.index is odd %}</p>

CC BY-SA 4.0
2018-08-20T17:33:55.073Z
Ah yes, makes sense now, perfect, thank you.
moondawg
CC BY-SA 4.0
2018-08-20T12:21:56.243Z
2018-08-20T12:21:56.243Z
Syversen
<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>

<matrix><forloop><loop><case>
Loop index inside a switch content builder
# 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" %}
    
        {% set images = block.imagePart %}

        {% if images and images|length %}
            {% set image = block.imagePart.all() %}

            {# Loop through the images #}
            {% for image in images.all() %}
                
                    
                
            {% endfor %}
        {% endif %}

        
            {{ block.textPart }}

**Tags:** matrix, forloop, loop, case

---

## 2 answers

### Answer by username_1 on 2018-08-20. Score: 1 | Accepted answer

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:
{% case "splitImageAndText" %}
    
        {% 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() %}
                
                    
                
            {% endfor %}
        {% endif %}

        {% if loop.index is odd %}
            {% include 'partials/text-block' %}
        {% endif %}
    

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:

    {{ block.textPart }}

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

---

