Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-23T10:12:34.310Z
Answers.LastActivityDate: 2019-10-23T10:21:32.907Z
Answers.LastEditDate: 2019-10-23T10:21:32.907Z
Answers.UserDisplayName: Oli
Body: <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>
  
Tags: <templating><matrix>
Title: Template output when grouping matrix blocks
ThreadText: # 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](https://github.com/craftcms/cms/issues/812#issuecomment-516975780) 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](https://www.dtott.com/thoughts/2016/06/30/nested-matrix-fields-craft-cms-workaround) but I haven't been able to get the template and queries right for this.
  
  I know I can use the [group filter](https://docs.craftcms.com/v2/templating/filters.html#group) 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 `<div>` for example, this should do it for you:
  
  ```twig
  {% set groupCount = 0 %}
  {% for block in entry.blocks %}
  
      {% switch block.type %}
          {% case "headingBlock" %}
              {{ groupCount > 0 ? '</div>' }}<div>
              {{ block.header }}
              {% set groupCount = groupCount + 1 %}
  
          {% case "richtextBlock" %}
              {{ block.richtext }}
  
          {% case "imageBlock" %}
              {{ block.image.one().url }}
      {% endswitch %}
  
  {% endfor %}
  </div>
  
  ```
  
  Changing the fields/blocks name accordingly of course.
  
  
  ---
  
  