<ol>
<li><p>Check if the <code>matrixField</code> is populated:</p>

<p><code>{% if entry.matrixField|length %}{% endif %}</code></p></li>
<li><p>There are two options to pull out fields from a block type. </p>

<p><strong>Option A:</strong> use a for-loop pointed at your Matrix field</p>

<pre><code>// If you want to output block of a certain type
{% if entry.matrixField|length %} 
    {% for block in entry.matrixField.type('yourBlock') %}
        {{ block.field1 }}
        {{ block.field2 }}
        {{ block.fieldN }}
    {% endfor %}
{% endif %}

// OR multiple types
{% if entry.matrixField|length %} 
    {% for block in entry.matrixField %}
        {% if block.type == "yourBlock" %}
            {{ block.field1 }}
            {{ block.field2 }}
            {{ block.fieldN }}
        {% elseif block.type == "yourBlock1" %}
            {{ block.field1 }}
            {{ block.field2 }}
            {{ block.fieldN }}
        {% endif %}
    {% endfor %}
{% endif %}
</code></pre>

<p><strong>Option B:</strong> use simplified method of a for-loop</p>

<pre><code>{% if entry.matrixField|length %} 
    {% set yourBlock = entry.matrixField.type('yourBlock') %}
    {% for block in entry.matrixField %}
        {% switch block.type %}
            {% case "yourBlock" %}
                {{ block.field1 }}
                {{ block.field2 }}
                {{ block.fieldN }}
        {% endswitch %}
    {% endfor %}
{% endif %}
</code></pre></li>
</ol>

<p>Also check Craft's documentation <a href="https://craftcms.com/docs/matrix-fields#templating" rel="noreferrer">about Matrix Fields</a>.</p>

CC BY-SA 3.0
2017-08-29T16:12:17.090Z
2017-08-29T16:12:17.090Z
Dominik Krulak
<p>I have a matrix field shaped as follows:</p>

<ul>
<li>MatrixField

<ul>
<li>Block ( just 1 blocktype )

<ul>
<li>Field1</li>
<li>Field2</li>
<li>Field3</li>
<li>Field4</li>
<li>FieldN</li>
</ul></li>
</ul></li>
</ul>

<p>I want to loop through all the fields inside a block, check if the field is set and other stuff. How can I do it?</p>

<matrix><field>
Loop through fields of a matrix block
# Loop through fields of a matrix block

(Asked by: username_0 on 2017-08-29)

I have a matrix field shaped as follows:

* MatrixField
  + Block ( just 1 blocktype )
    - Field1
    - Field2
    - Field3
    - Field4
    - FieldN

I want to loop through all the fields inside a block, check if the field is set and other stuff. How can I do it?

**Tags:** matrix, field


---

## 1 answers

### Answer by username_1 on 2017-08-29. Score: 6 | Accepted answer

Check if the matrixField is populated:
{% if entry.matrixField|length %}{% endif %}
There are two options to pull out fields from a block type. 
Option A: use a for-loop pointed at your Matrix field
// If you want to output block of a certain type
{% if entry.matrixField|length %} 
    {% for block in entry.matrixField.type('yourBlock') %}
        {{ block.field1 }}
        {{ block.field2 }}
        {{ block.fieldN }}
    {% endfor %}
{% endif %}

// OR multiple types
{% if entry.matrixField|length %} 
    {% for block in entry.matrixField %}
        {% if block.type == "yourBlock" %}
            {{ block.field1 }}
            {{ block.field2 }}
            {{ block.fieldN }}
        {% elseif block.type == "yourBlock1" %}
            {{ block.field1 }}
            {{ block.field2 }}
            {{ block.fieldN }}
        {% endif %}
    {% endfor %}
{% endif %}

Option B: use simplified method of a for-loop
{% if entry.matrixField|length %} 
    {% set yourBlock = entry.matrixField.type('yourBlock') %}
    {% for block in entry.matrixField %}
        {% switch block.type %}
            {% case "yourBlock" %}
                {{ block.field1 }}
                {{ block.field2 }}
                {{ block.fieldN }}
        {% endswitch %}
    {% endfor %}
{% endif %}


Also check Craft's documentation about Matrix Fields.


---

