<p>It is possible to set custom field parameters on Matrix block queries (→ <a href="http://buildwithcraft.com/updates#build2615" rel="nofollow">Craft 2.3 release notes</a>). And looking for non empty fields is possible with <code>:nonempty:</code> (also not documented but mentioned in the <a href="http://buildwithcraft.com/updates#build2524" rel="nofollow">2.0 release notes</a>).</p>

<pre><code>{% set blocksWithTitle = entry.myMatrixField.myTitleField(':notempty:') %}

{% for block in blocksWithTitle %}
    {{ block.myTitleField }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-13T14:40:49.507Z
2015-04-13T14:40:49.507Z
carlcs
<p>The most reliable method i've found to test if a matrix field is defined is using the <code>length</code> twig filter:</p>

<pre><code>{% if entry.myMatrixField | length %}
  {% for block in entry.myMatrixField %}
    &lt;p&gt;Matrix blocks here.&lt;/p&gt;
  {% endfor %}
{% else %}
  &lt;p&gt;Matrix not defined.&lt;/p&gt;
{% endif %}
</code></pre>

<p>More on this in the Craft CMS docs <a href="https://craftcms.com/docs/matrix-fields#getting-the-total-number-of-blocks" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2018-05-03T11:06:52.860Z
I believe that `entry.myMatrixField.first()` (or `entry.myMatrixField.one()` in Craft v3) is more efficient, because it doesn't have to count them all up.
ianp
CC BY-SA 3.0
2017-02-08T19:05:34.593Z
2017-02-09T21:56:56.160Z
2017-02-09T21:56:56.160Z
coryetzkorn
<p>This should work:</p>

<pre><code>{% if block['blockTitle'] is defined %}
    // Do your thing
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-04-13T13:37:35.213Z
2015-04-13T13:37:35.213Z
Mats Mikkel Rummelhoff
<p>When looping through a matrix, is there aquick way to check whether or not the block has a particular field (not if the field is empty, but if the field exists at all).</p>

<p>I have a matrix with ~20 block types, 12 or so have a field called blockTitle. For in-page nav, I'd like to loop through all the blocks and just output blockTitle for those that do have it. I could do this with a conditional for block.type, but am wondering if there's a simpler check (in case we add more block types in the future, don't want to have to update the conditional every time).</p>

<p>Using {% if block.blockTitle %} will kick back "Craft\MatrixBlockModel and its behaviors do not have a method or closure named "blockTitle"." for block types without that field, so that's a no go.</p>

<p>Tks.</p>

<templating><matrix>
Check to See if Field Exists
# Check to See if Field Exists

(Asked by: username_0 on 2015-04-13)

When looping through a matrix, is there aquick way to check whether or not the block has a particular field (not if the field is empty, but if the field exists at all).

I have a matrix with ~20 block types, 12 or so have a field called blockTitle. For in-page nav, I'd like to loop through all the blocks and just output blockTitle for those that do have it. I could do this with a conditional for block.type, but am wondering if there's a simpler check (in case we add more block types in the future, don't want to have to update the conditional every time).

Using {% if block.blockTitle %} will kick back "Craft\MatrixBlockModel and its behaviors do not have a method or closure named "blockTitle"." for block types without that field, so that's a no go.

Tks.

**Tags:** templating, matrix


---

## 3 answers

### Answer by username_4 on 2015-04-13. Score: 16 | Accepted answer

This should work:
{% if block['blockTitle'] is defined %}
    // Do your thing
{% endif %}


---

### Answer by username_1 on 2015-04-13. Score: 2

It is possible to set custom field parameters on Matrix block queries (→ Craft 2.3 release notes). And looking for non empty fields is possible with :nonempty: (also not documented but mentioned in the 2.0 release notes).
{% set blocksWithTitle = entry.myMatrixField.myTitleField(':notempty:') %}

{% for block in blocksWithTitle %}
    {{ block.myTitleField }}
{% endfor %}


---

### Answer by username_2 on 2017-02-08. Score: 2

The most reliable method i've found to test if a matrix field is defined is using the length twig filter:
{% if entry.myMatrixField | length %}
  {% for block in entry.myMatrixField %}
    Matrix blocks here.
  {% endfor %}
{% else %}
  Matrix not defined.
{% endif %}

More on this in the Craft CMS docs here.

> **Comments:**
> **username_3**: I believe that `entry.myMatrixField.first()` (or `entry.myMatrixField.one()` in Craft v3) is more efficient, because it doesn't have to count them all up.  

---

