<p>The right syntax to filter matrix blocks by block type is this (see <a href="http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type" rel="nofollow">Craft docs here</a>):</p>

<pre><code>{% for block in entry.myMatrixField.type('text') %}{% endfor %}
</code></pre>

<p>So you only have to remove the extra <code>.block</code> you have in your example code. I'd save the returned array of MatrixBlockModels to a variable and use the <code>length</code> filter to see if there are any blocks of this type.</p>

<pre><code>{% set twoSeaterSofas = entry.sofa.type('twoSeaterSofa') %}
{% set threeSeaterSofas = entry.sofa.type('threeSeaterSofas') %}

&lt;!-- some HTML --&gt;

{% if twoSeaterSofas|length %}
    {% for block in twoSeaterSofas %}    
    {% endfor %}
{% endif %}

&lt;!-- more HTML --&gt;

{% if threeSeaterSofas|length %}
    {% for block in threeSeaterSofas %}    
    {% endfor %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-01-26T12:31:32.520Z
2015-01-26T12:31:32.520Z
carlcs
<p>There are multiple ways of doing this, this is mine:</p>

<pre><code>{# Loop through all blocks and include a file with the block's type(slug) #}
{% for block in entry.matrixFieldHandle %}
    {% include "includes/blockTypes/_" ~ block.type ignore missing %}
{% endfor %}
</code></pre>

<p>So, you need a folder, for instance <code>blockTypes</code>, within a folder <code>includes</code> (or whatever path you'd want to give those), with a file for every Matrix block type, prepended with '_'. I find this easy to work with, especially when dealing with large numbers of types.</p>

<p>The <code>ignore missing</code> part of the include makes sure that you don't get errors if a file is not found. So, if nothing gets displayed, chances are one or more files don't exist.</p>

<p>Within your include, you only use the code needed to show that type's fields:</p>

<pre><code>{{ block.price }}
</code></pre>

CC BY-SA 3.0
2015-01-26T15:20:49.860Z
Did I get the question wrong or isn't he trying to group the matrix blocks by block type?
carlcs
CC BY-SA 3.0
2015-01-26T15:21:43.470Z
carlcs, I think you're right! Ignore my answer, then ;)
Paul
CC BY-SA 3.0
2015-01-26T11:18:05.467Z
2015-01-26T11:18:05.467Z
Paul
<p>Is this the most efficient way to use matrix in a template where different block types need outputting in various area of the HTML markup? I have 9 block types that need to be placed in various areas of the HTML markup.</p>

<pre><code>{# Create a variable with the matrix fields in #}
{% set var = entry.sofa %}

{# Is the block in the variable #}
{% if var.block.type('twoSeaterSofa') %}

    {# block is in variable #}
    {% for block in var.block.type('twoSeaterSofa') %}      
        {# output block #}
        {{ block.price }}
    {% endfor %}

{% endif %}
</code></pre>

<p>Grateful for any advice or code.</p>

<templating><matrix>
What is the most efficient way to code matrix blocks by type in a template?
# What is the most efficient way to code matrix blocks by type in a template?

(Asked by: username_0 on 2015-01-26)

Is this the most efficient way to use matrix in a template where different block types need outputting in various area of the HTML markup? I have 9 block types that need to be placed in various areas of the HTML markup.
{# Create a variable with the matrix fields in #}
{% set var = entry.sofa %}

{# Is the block in the variable #}
{% if var.block.type('twoSeaterSofa') %}

    {# block is in variable #}
    {% for block in var.block.type('twoSeaterSofa') %}      
        {# output block #}
        {{ block.price }}
    {% endfor %}

{% endif %}

Grateful for any advice or code.

**Tags:** templating, matrix

---

## 2 answers

### Answer by username_1 on 2015-01-26. Score: 3 | Accepted answer

The right syntax to filter matrix blocks by block type is this (see Craft docs here):
{% for block in entry.myMatrixField.type('text') %}{% endfor %}

So you only have to remove the extra .block you have in your example code. I'd save the returned array of MatrixBlockModels to a variable and use the length filter to see if there are any blocks of this type.
{% set twoSeaterSofas = entry.sofa.type('twoSeaterSofa') %}
{% set threeSeaterSofas = entry.sofa.type('threeSeaterSofas') %}



{% if twoSeaterSofas|length %}
    {% for block in twoSeaterSofas %}    
    {% endfor %}
{% endif %}



{% if threeSeaterSofas|length %}
    {% for block in threeSeaterSofas %}    
    {% endfor %}
{% endif %}


---

### Answer by username_2 on 2015-01-26. Score: 2

There are multiple ways of doing this, this is mine:
{# Loop through all blocks and include a file with the block's type(slug) #}
{% for block in entry.matrixFieldHandle %}
    {% include "includes/blockTypes/_" ~ block.type ignore missing %}
{% endfor %}

So, you need a folder, for instance blockTypes, within a folder includes (or whatever path you'd want to give those), with a file for every Matrix block type, prepended with '_'. I find this easy to work with, especially when dealing with large numbers of types.
The ignore missing part of the include makes sure that you don't get errors if a file is not found. So, if nothing gets displayed, chances are one or more files don't exist.
Within your include, you only use the code needed to show that type's fields:
{{ block.price }}

> **Comments:**
> **username_1**: Did I get the question wrong or isn't he trying to group the matrix blocks by block type?  
> **username_2**: carlcs, I think you're right! Ignore my answer, then ;)  

---

