<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>

CC BY-SA 3.0
2015-05-06T22:57:51.267Z
2015-05-06T22:57:51.267Z
Brad Bell
<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>

CC BY-SA 3.0
2015-05-07T01:22:25.047Z
Thanks, this fixes the issue for the time being. I really appreciate the help!
user1204
CC BY-SA 3.0
2015-05-07T05:06:02.237Z
No problem – happy it worked for you, @user1204 :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-05-06T22:45:31.080Z
2015-05-06T23:05:31.093Z
2015-05-06T23:05:31.093Z
Mats Mikkel Rummelhoff
<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>

CC BY-SA 3.0
2015-05-06T15:49:19.580Z
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?
Jamie Wade
CC BY-SA 3.0
2015-05-06T15:56:01.017Z
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
Jamie Wade
CC BY-SA 3.0
2015-05-06T16:04:40.637Z
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".`
user1204
CC BY-SA 3.0
2015-05-06T16:07:58.780Z
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?
Jamie Wade
CC BY-SA 3.0
2015-05-06T16:15:51.547Z
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.
user1204
CC BY-SA 3.0
2015-05-06T16:18:28.587Z
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 :)
Jamie Wade
<matrix>
Checking for existence of a field within a Matrix block
# 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 %}
                    Uh oh! This field doesn't actually exist.
                {% endif %}

                
                    {{ block.quote }}
                    – {{ block.attribution }}
                

            {% 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 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".`  
> **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, @user1204 :)  

---

### 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.


---

