1

I have a matrix field that has two different block types. The matrix field is on an entry that lists a number of benefits for a given product on a corporate website. The two different blocks are firstly an "intro" block that has a couple of text fields for a headline and description and then secondly a "benefits" to create a list of benefits. I then output the items in the template as follows:

{% for intro in entry.benefits %}
    {% for image in intro.leadImage %}
        <img class="headImage" src="{{ image.getUrl() }}" width="{{ image.getWidth() }}" height="{{ image.getHeight() }}" alt="{{ image.title }}">
    {% endfor %}
    <h3 class="sectionHead">{{ intro.introTitle }}</h3>
    <p class="intro">{{ intro.introParagraph }}</p>
{% endfor %}

<div class="details">
    {% for benefitsItem in entry.benefits %}
        <p><span class="orange">{{ benefitsItem.benefit }}</span> - {{ benefitsItem.description }}</p>
    {% endfor %}
</div>

There are two different for loops, one that lists the "intro" block and one that lists the "benefitsItem" block. The problem I have is that I want to be able to separate lists of benefits into sections by inserting another "intro" block in the CMS content but of course no matter where I order my "intro" blocks in the admin they will always appear above all the benefits where the for loop puts them out. Does anyone know how I would go about creating a matrix field or changing my template so that I can order things as follows:

Intro
Benefit
Benefit

Intro
Benefit
Benefit

Any ideas greatly appreciated. Thanks.

1 Answer 1

2

It might be worth checking the types of the matrix block like:

{% for block in entry.benefits.all() %}
    {% switch block.type %}
        {% case "intro" %} {# <=== ASSUMING BLOCK TYPE HANDLE #}
            {% for image in block.leadImage.all() %}
                <img class="headImage" src="{{ image.getUrl() }}" width="{{ image.getWidth() }}" height="{{ image.getHeight() }}" alt="{{ image.title }}">
            {% endfor %}
            <h3 class="sectionHead">{{ block.introTitle }}</h3>
            <p class="intro">{{ block.introParagraph }}</p>

        {% case "details" %} {# <=== ASSUMING BLOCK TYPE HANDLE #}
            <div class="details">
                <p><span class="orange">{{ block.benefit }}</span> - {{ block.description }}</p>
            </div>

    {% endswitch %}
{% endfor %}

If you're wanting a list inside of a matrix (matrix in a matrix for the Benefits), I'd suggest killing the Benefits block type and adding a SuperTable or Neo field to the Intro block type then you'll be able to list the benefits below each intro in a for loop.

4
  • The SuperTable solution seems like a good solution, although the problem I'm facing now is how to output it in my template. I created a new matrix field with the handle "featuresList" with only one block called "listOfFeatures". Inside that block is plain text block called "heading", an asset field called "introImage" and a redactor field called "introParagraph" that all output fine. The fourth field is a super table set to display as a matrix with the handle "features" and two fields called "featureName" and "featureDescription" ...
    – JamiePatt
    Feb 26, 2019 at 13:51
  • however in my template anything like "{% for row in features %}" generates a twig error: "Variable "features" does not exist." I've tried using the handle from the block that the supertable sits inside of and no luck. Have you templated a super table before?
    – JamiePatt
    Feb 26, 2019 at 13:52
  • Okay, so now I've gotten rid of the error using {% for row in block.features %}, and I know it is outputting something for each row but for some reason using block.featureName and block.featureDescription does not get the content from the database, It's essentially outputting the container html that I have in my template for each row but with no text. Any idea what I'm doing wrong?
    – JamiePatt
    Feb 26, 2019 at 14:04
  • Try doing {{ row.featureName }} instead of {{ block.featureName }}, you've already used block when calling the parent matrix, when you're looping through the SuperTable field like {% for row in block.features %} you will need to use row to access "featureName" & "featureDescription" Feb 26, 2019 at 15:04

Your Answer

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

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