1

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?

1 Answer 1

1

If by grouping you mean wrapping each groups in a <div> for example, this should do it for you:

{% 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.