Answers.Body: <p>This is a known bug we've tracked back to Twig. We've got it on our list to look into, and hopefully provide a patch and tests to Sensio, but haven't gotten to it, yet.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-06T22:57:51.267Z
Answers.LastActivityDate: 2015-05-06T22:57:51.267Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Use brackets instead of dot notation for the field handle when you test for <code>defined</code>:</p>
  
  <pre><code>...
  {% if block['test'] is defined %}
      ...
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T01:22:25.047Z
Answers.Text: Thanks, this fixes the issue for the time being. I really appreciate the help!
Answers.UserDisplayName: user1204
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T05:06:02.237Z
Answers.Text: No problem – happy it worked for you, @user1204 :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-06T22:45:31.080Z
Answers.LastActivityDate: 2015-05-06T23:05:31.093Z
Answers.LastEditDate: 2015-05-06T23:05:31.093Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>In trying to test for the existence of a field within a Matrix block I think I may have stumbled upon a bug, or I'm missing something and approaching this the wrong way.</p>
  
  <p>I've setup a simple test using a fresh, standard Craft install with a very basic Matrix setup. I've pasted the Matrix template code below.</p>
  
  <pre><code>    {% if entry.matrix is defined and entry.matrix|length %}
  
          {% for block in entry.matrix %}
  
              {% if block.type == "richText" %}
  
                  {{ block.richText }}
  
              {% elseif block.type == "plainText" %}
  
                  {{ block.plainText|markdown }}
  
              {% elseif block.type == "blockquote" %}
  
                  {% if block.test is defined %}
                      &lt;p&gt;&lt;strong&gt;Uh oh! This field doesn't actually exist.&lt;/strong&gt;&lt;/p&gt;
                  {% endif %}
  
                  &lt;blockquote&gt;
                      &lt;p&gt;{{ block.quote }}&lt;/p&gt;
                      &lt;cite&gt;– {{ block.attribution }}&lt;/cite&gt;
                  &lt;/blockquote&gt;
  
              {% endif %}
  
          {% endfor %}
  
      {% endif %}
  </code></pre>
  
  <p>Within the blockquote block I am checking if <code>block.test</code> is defined, this check always evaluates to true despite the fact that it doesn't exist and if I add any additional checks it bombs out the template.</p>
  
  <p>Any insight is greatly appreciated!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-06T15:49:19.580Z
Comments.Text: Welcome to Craft, can you provide a bit more context here? What is `block.test`, and why are you checking for a field that doesn't exist?
Comments.UserDisplayName: Jamie Wade
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-06T15:56:01.017Z
Comments.Text: Also instead of checking `{% if block.test is defined %}`, try using `{% if block.test|length %}`. Have a read of this if you haven't already: http://craftcms.stackexchange.com/questions/5482/how-can-i-tell-if-a-field-exists-in-a-block-of-a-matrix/5501#5501
Comments.UserDisplayName: Jamie Wade
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-06T16:04:40.637Z
Comments.Text: Thanks Jamie, I have tried using the length filter, but that throws an exception.
  
  `Craft\MatrixBlockModel and its behaviors do not have a method or closure named "test".`
Comments.UserDisplayName: user1204
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-06T16:07:58.780Z
Comments.Text: OK then, so I presume there isn't a field called `test` in your matrix field? Which still has me confused as to why you're checking if a field is defined, but that field doesn't exist? Can you explain a little more please?
Comments.UserDisplayName: Jamie Wade
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-06T16:15:51.547Z
Comments.Text: For a little more context on what I'm trying to do. I have a template for each of my Matrix block types and I'm trying to reuse those templates even though there could be slight variations of blocks across different Matrix fields. For instance, in one of my Matrix fields I could have an image block that includes an alignment field and in another Matrix field I could have an image block without an alignment field, but I'd like to reuse the image block template for both.
Comments.UserDisplayName: user1204
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-06T16:18:28.587Z
Comments.Text: That makes perfect sense, thanks for clarifying. I'll have to have a think and come back to you, but I'm sure someone else will have an answer by then :)
Comments.UserDisplayName: Jamie Wade
Tags: <matrix>
Title: Checking for existence of a field within a Matrix block
ThreadText: # Checking for existence of a field within a Matrix block
  
  (Asked by: username_0 on 2015-05-06)
  
  In trying to test for the existence of a field within a Matrix block I think I may have stumbled upon a bug, or I'm missing something and approaching this the wrong way.
  
  I've setup a simple test using a fresh, standard Craft install with a very basic Matrix setup. I've pasted the Matrix template code below.
  
  ```
      {% if entry.matrix is defined and entry.matrix|length %}
  
          {% for block in entry.matrix %}
  
              {% if block.type == "richText" %}
  
                  {{ block.richText }}
  
              {% elseif block.type == "plainText" %}
  
                  {{ block.plainText|markdown }}
  
              {% elseif block.type == "blockquote" %}
  
                  {% if block.test is defined %}
                      <p><strong>Uh oh! This field doesn't actually exist.</strong></p>
                  {% endif %}
  
                  <blockquote>
                      <p>{{ block.quote }}</p>
                      <cite>– {{ block.attribution }}</cite>
                  </blockquote>
  
              {% endif %}
  
          {% endfor %}
  
      {% endif %}
  
  ```
  
  Within the blockquote block I am checking if `block.test` is defined, this check always evaluates to true despite the fact that it doesn't exist and if I add any additional checks it bombs out the template.
  
  Any insight is greatly appreciated!
  
  **Tags:** matrix
  
  > **Comments:**
  > **username_1**: Welcome to Craft, can you provide a bit more context here? What is `block.test`, and why are you checking for a field that doesn't exist?  
  > **username_1**: Also instead of checking `{% if block.test is defined %}`, try using `{% if block.test|length %}`. Have a read of this if you haven't already: http://craftcms.stackexchange.com/questions/5482/how-can-i-tell-if-a-field-exists-in-a-block-of-a-matrix/5501#5501  
  > **username_0**: Thanks username_1, I have tried using the length filter, but that throws an exception.  `Craft\MatrixBlockModel and its behaviors do not have a method or closure named "test".`  
  > **username_1**: OK then, so I presume there isn't a field called `test` in your matrix field? Which still has me confused as to why you're checking if a field is defined, but that field doesn't exist? Can you explain a little more please?  
  > **username_0**: For a little more context on what I'm trying to do. I have a template for each of my Matrix block types and I'm trying to reuse those templates even though there could be slight variations of blocks across different Matrix fields. For instance, in one of my Matrix fields I could have an image block that includes an alignment field and in another Matrix field I could have an image block without an alignment field, but I'd like to reuse the image block template for both.  
  > **username_1**: That makes perfect sense, thanks for clarifying. I'll have to have a think and come back to you, but I'm sure someone else will have an answer by then :)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2015-05-06. Score: 11 | Accepted answer
  
  Use brackets instead of dot notation for the field handle when you test for `defined`:
  
  ```
  ...
  {% if block['test'] is defined %}
      ...
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, this fixes the issue for the time being. I really appreciate the help!  
  > **username_3**: No problem – happy it worked for you, @username_0 :)  
  
  ---
  
  ### Answer by username_2 on 2015-05-06. Score: 3
  
  This is a known bug we've tracked back to Twig. We've got it on our list to look into, and hopefully provide a patch and tests to Sensio, but haven't gotten to it, yet.
  
  
  ---
  
  