<p>It might be worth checking the types of the matrix block like:</p>

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

        {% case "details" %} {# &lt;=== ASSUMING BLOCK TYPE HANDLE #}
            &lt;div class="details"&gt;
                &lt;p&gt;&lt;span class="orange"&gt;{{ block.benefit }}&lt;/span&gt; - {{ block.description }}&lt;/p&gt;
            &lt;/div&gt;

    {% endswitch %}
{% endfor %}

</code></pre>

<p>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 <a href="https://plugins.craftcms.com/super-table" rel="nofollow noreferrer">SuperTable</a> or <a href="https://plugins.craftcms.com/neo" rel="nofollow noreferrer">Neo</a> field to the Intro block type then you'll be able to list the benefits below each intro in a for loop.</p>

CC BY-SA 4.0
2019-02-26T13:51:28.197Z
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
CC BY-SA 4.0
2019-02-26T13:52:37.250Z
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
CC BY-SA 4.0
2019-02-26T14:04:10.847Z
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
CC BY-SA 4.0
2019-02-26T15:04:51.953Z
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"
Jamie Jenkins
CC BY-SA 4.0
2019-02-26T11:50:49.167Z
2019-02-26T16:18:46.413Z
2019-02-26T16:18:46.413Z
Jamie Jenkins
<p>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 &quot;intro&quot; block that has a couple of text fields for a headline and description and then secondly a &quot;benefits&quot; to create a list of benefits. I then output the items in the template as follows:</p>

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

&lt;div class=&quot;details&quot;&gt;
    {% for benefitsItem in entry.benefits %}
        &lt;p&gt;&lt;span class=&quot;orange&quot;&gt;{{ benefitsItem.benefit }}&lt;/span&gt; - {{ benefitsItem.description }}&lt;/p&gt;
    {% endfor %}
&lt;/div&gt;
</code></pre>
<p>There are two different for loops, one that lists the &quot;intro&quot; block and one that lists the &quot;benefitsItem&quot; block. The problem I have is that I want to be able to separate lists of benefits into sections by inserting another &quot;intro&quot; block in the CMS content but of course no matter where I order my &quot;intro&quot; 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:</p>
<blockquote>
<p>Intro<br>
Benefit<br>
Benefit<br></p>
<p>Intro<br>
Benefit<br>
Benefit<br></p>
</blockquote>
<p>Any ideas greatly appreciated. Thanks.</p>

<templating><matrix>
Templating repeatable blocks in a matrix field
# Templating repeatable blocks in a matrix field

(Asked by: username_0 on 2019-02-26)

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 %}
        
    {% endfor %}
    {{ intro.introTitle }}
    {{ intro.introParagraph }}
{% endfor %}


    {% for benefitsItem in entry.benefits %}
        {{ benefitsItem.benefit }} - {{ benefitsItem.description }}
    {% endfor %}


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.

**Tags:** templating, matrix


---

## 1 answers

### Answer by username_1 on 2019-02-26. Score: 2 | Accepted answer

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() %}
                
            {% endfor %}
            {{ block.introTitle }}
            {{ block.introParagraph }}

        {% case "details" %} {# <=== ASSUMING BLOCK TYPE HANDLE #}
            
                {{ block.benefit }} - {{ block.description }}
            

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

> **Comments:**
> **username_0**: 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" ...  
> **username_0**: 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?  
> **username_0**: 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?  
> **username_1**: 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"  

---

