Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-13T14:40:49.507Z
Answers.LastActivityDate: 2015-04-13T14:40:49.507Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-03T11:06:52.860Z
Answers.Text: 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.
Answers.UserDisplayName: ianp
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-08T19:05:34.593Z
Answers.LastActivityDate: 2017-02-09T21:56:56.160Z
Answers.LastEditDate: 2017-02-09T21:56:56.160Z
Answers.UserDisplayName: coryetzkorn
Answers.Body: <p>This should work:</p>
  
  <pre><code>{% if block['blockTitle'] is defined %}
      // Do your thing
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-13T13:37:35.213Z
Answers.LastActivityDate: 2015-04-13T13:37:35.213Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <templating><matrix>
Title: Check to See if Field Exists
ThreadText: # 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](http://buildwithcraft.com/updates#build2615)). And looking for non empty fields is possible with `:nonempty:` (also not documented but mentioned in the [2.0 release notes](http://buildwithcraft.com/updates#build2524)).
  
  ```
  {% 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 %}
      <p>Matrix blocks here.</p>
    {% endfor %}
  {% else %}
    <p>Matrix not defined.</p>
  {% endif %}
  
  ```
  
  More on this in the Craft CMS docs [here](https://craftcms.com/docs/matrix-fields#getting-the-total-number-of-blocks).
  
  > **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.  
  
  ---
  
  