<p>A possible solution could be this:
Instead of outputting your blockType3 immediately, save all blockType3 ID's in an array. When your loop has finished (check for loop.last) output all the blocktypes from the array at once.</p>

<p>This code is just as an example (untested):</p>

<pre><code>{% set arrBlockType3 = [] %}
{% for block in entry.matrixField.all() %}
    {% switch block.type %}
        {% case 'blockType1' %}
            &lt;div&gt;
                {{ block.field }}
            &lt;/div&gt;
            {% case 'blockType2' %}
            &lt;div&gt;
                {{ block.field }}
            &lt;/div&gt;
            {% case 'blockType3' %}
                {% set arrBlockType3 = arrBlockType3 | merge([block.id]) %}
    {% endswitch %}
    {% if loop.last %}
        &lt;ul&gt;
        {% for id in arrBlockType3 %}
            {% set block = entry.matrixField.id(id).first() %}
            &lt;li&gt;{{ block.field }}&lt;/li&gt;
        {% endfor %}
        &lt;/ul&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-09-28T07:50:55.067Z
2018-09-28T07:50:55.067Z
Tom De Smet
<p>I made a template including a matrix field. I want to wrap all occurrences of a certain block type into a <code>&lt;ul&gt;</code> element. How can I do this?</p>

<p>My current code:</p>

<pre><code> {% for block in entry.matrixField.all() %}
        {% switch block.type %}
            {% case 'blockType1' %}
            &lt;div&gt;
                {{ block.field }}
            &lt;/div&gt;
            {% case 'blockType2' %}
            &lt;div&gt;
                {{ block.field }}
            &lt;/div&gt;
            {% case 'blockType3' %}
            &lt;ul&gt;
                &lt;li&gt;
                    {{ block.field }}
                &lt;/li&gt;
            &lt;/ul&gt;
        {% endswitch %}
    {% endfor %}
</code></pre>

<p>Current output:</p>

<pre><code>&lt;ul&gt;
    &lt;li&gt;blockfield&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;blockfield&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;blockfield&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>But I want my output like this:</p>

<pre><code>&lt;ul&gt;
    &lt;li&gt;blockfield&lt;/li&gt;
    &lt;li&gt;blockfield&lt;/li&gt;
    &lt;li&gt;blockfield&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>That is, I want the <code>ul</code> tag to follow the loop of blockType3.</p>

CC BY-SA 4.0
2018-10-28T08:20:24.443Z
This question has some tips on how to dynamically wrap block types that are of the same type if they’re next to each other. https://craftcms.stackexchange.com/q/1745/50
Simon Kuran
<templating><matrix>
How to wrap a matrix blocktype occurrence loop inside a ul?
# How to wrap a matrix blocktype occurrence loop inside a ul?

(Asked by: username_0 on 2018-09-24)

I made a template including a matrix field. I want to wrap all occurrences of a certain block type into a  element. How can I do this?
My current code:
 {% for block in entry.matrixField.all() %}
        {% switch block.type %}
            {% case 'blockType1' %}
            
                {{ block.field }}
            
            {% case 'blockType2' %}
            
                {{ block.field }}
            
            {% case 'blockType3' %}
            
                
                    {{ block.field }}
                
            
        {% endswitch %}
    {% endfor %}

Current output:

    blockfield


    blockfield


    blockfield


But I want my output like this:

    blockfield
    blockfield
    blockfield


That is, I want the ul tag to follow the loop of blockType3.

**Tags:** templating, matrix

> **Comments:**
> **username_1**: This question has some tips on how to dynamically wrap block types that are of the same type if they’re next to each other. https://craftcms.stackexchange.com/q/1745/50  

---

## 1 answers

### Answer by username_2 on 2018-09-28. Score: 2

A possible solution could be this:
Instead of outputting your blockType3 immediately, save all blockType3 ID's in an array. When your loop has finished (check for loop.last) output all the blocktypes from the array at once.
This code is just as an example (untested):
{% set arrBlockType3 = [] %}
{% for block in entry.matrixField.all() %}
    {% switch block.type %}
        {% case 'blockType1' %}
            
                {{ block.field }}
            
            {% case 'blockType2' %}
            
                {{ block.field }}
            
            {% case 'blockType3' %}
                {% set arrBlockType3 = arrBlockType3 | merge([block.id]) %}
    {% endswitch %}
    {% if loop.last %}
        
        {% for id in arrBlockType3 %}
            {% set block = entry.matrixField.id(id).first() %}
            {{ block.field }}
        {% endfor %}
        
    {% endif %}
{% endfor %}


---

