<p>If by grouping you mean wrapping each <em>groups</em> in a <code>&lt;div&gt;</code> for example, this should do it for you:</p>

<pre class="lang-twig prettyprint-override"><code>{% set groupCount = 0 %}
{% for block in entry.blocks %}

    {% switch block.type %}
        {% case "headingBlock" %}
            {{ groupCount &gt; 0 ? '&lt;/div&gt;' }}&lt;div&gt;
            {{ block.header }}
            {% set groupCount = groupCount + 1 %}

        {% case "richtextBlock" %}
            {{ block.richtext }}

        {% case "imageBlock" %}
            {{ block.image.one().url }}
    {% endswitch %}

{% endfor %}
&lt;/div&gt;
</code></pre>

<p>Changing the fields/blocks name accordingly of course.</p>

CC BY-SA 4.0
2019-10-23T10:12:34.310Z
2019-10-23T10:21:32.907Z
2019-10-23T10:21:32.907Z
Oli
<p>I have a shared content builder matrix field that has the usual blocks: Redactor, Headings, Images, etc. </p>

<p>A typical entry might look like this:</p>

<pre><code>- Heading
- Redactor
- Image
- Redactor
- Heading
- Redactor
- Heading
- Redactor
- Image
</code></pre>

<p>Ideally I'd use a nested matrix - I'm <a href="https://github.com/craftcms/cms/issues/812#issuecomment-516975780" rel="nofollow noreferrer">aware this is on the roadmap</a>  and Super Table / Neo do support this but I'd like to reuse my shared content builder field and handle the output via the templates. </p>

<p>On the page I'd like to output it like so:</p>

<pre><code>- Heading | - Redactor
          | - Image
          | - Redactor

- Heading | - Redactor
- Heading | - Redactor
- Heading | - Redactor
            - Image
</code></pre>

<p>Basically grouping by heading block and then output any content until the next heading block. </p>

<p>I've seen a couple of examples which are <a href="https://www.dtott.com/thoughts/2016/06/30/nested-matrix-fields-craft-cms-workaround" rel="nofollow noreferrer">similar to this</a> but I haven't been able to get the template and queries right for this. </p>

<p>I know I can use the <a href="https://docs.craftcms.com/v2/templating/filters.html#group" rel="nofollow noreferrer">group filter</a> but I'm having trouble with not being able to <code>break</code> out of a loop. Fairly sure I need to construct a list of structured <code>id</code>'s and output them that way. </p>

<p>Anyone done something similar to this or can shed some light?</p>

<templating><matrix>
Template output when grouping matrix blocks
# Template output when grouping matrix blocks

(Asked by: username_0 on 2019-10-21)

I have a shared content builder matrix field that has the usual blocks: Redactor, Headings, Images, etc. 
A typical entry might look like this:
- Heading
- Redactor
- Image
- Redactor
- Heading
- Redactor
- Heading
- Redactor
- Image

Ideally I'd use a nested matrix - I'm aware this is on the roadmap  and Super Table / Neo do support this but I'd like to reuse my shared content builder field and handle the output via the templates. 
On the page I'd like to output it like so:
- Heading | - Redactor
          | - Image
          | - Redactor

- Heading | - Redactor
- Heading | - Redactor
- Heading | - Redactor
            - Image

Basically grouping by heading block and then output any content until the next heading block. 
I've seen a couple of examples which are similar to this but I haven't been able to get the template and queries right for this. 
I know I can use the group filter but I'm having trouble with not being able to break out of a loop. Fairly sure I need to construct a list of structured id's and output them that way. 
Anyone done something similar to this or can shed some light?

**Tags:** templating, matrix


---

## 1 answers

### Answer by username_1 on 2019-10-23. Score: 1

If by grouping you mean wrapping each groups in a  for example, this should do it for you:
{% set groupCount = 0 %}
{% for block in entry.blocks %}

    {% switch block.type %}
        {% case "headingBlock" %}
            {{ groupCount > 0 ? '' }}
            {{ block.header }}
            {% set groupCount = groupCount + 1 %}

        {% case "richtextBlock" %}
            {{ block.richtext }}

        {% case "imageBlock" %}
            {{ block.image.one().url }}
    {% endswitch %}

{% endfor %}


Changing the fields/blocks name accordingly of course.


---

