Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-26T12:31:32.520Z
Answers.LastActivityDate: 2015-01-26T12:31:32.520Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-26T15:20:49.860Z
Answers.Text: Did I get the question wrong or isn't he trying to group the matrix blocks by block type?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-26T15:21:43.470Z
Answers.Text: carlcs, I think you're right! Ignore my answer, then ;)
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-26T11:18:05.467Z
Answers.LastActivityDate: 2015-01-26T11:18:05.467Z
Answers.UserDisplayName: Paul
Body: <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>
  
Tags: <templating><matrix>
Title: What is the most efficient way to code matrix blocks by type in a template?
ThreadText: # 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](http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type)):
  
  ```
  {% 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') %}
  
  <!-- some HTML -->
  
  {% if twoSeaterSofas|length %}
      {% for block in twoSeaterSofas %}    
      {% endfor %}
  {% endif %}
  
  <!-- more HTML -->
  
  {% 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**: username_1, I think you're right! Ignore my answer, then ;)  
  
  ---
  
  