<p>In certain cases Twig is unable to recognize the correct object oriented syntax with magic properties so <code>{% if object.property is defined %}</code> will still produce an error.</p>

<p>You can use <code>{% if block['backgroundColor'] is defined %}</code> it seems strange and would usually considered as wrong syntax (PHP wise) but it works in Twig</p>

CC BY-SA 4.0
2018-06-05T11:53:38.920Z
2018-06-05T11:53:38.920Z
Robin Schambach
<p>Situation: I have a matrix with about 10 block types, 3 of which have a certain field. I want to check if the current block has that field.</p>

<p>The field in question is a dropdown with values in all 3 options.</p>

<p>I've tried:</p>

<ul>
<li><code>{{ attribute(block, 'backgroundColor') is defined ? 'true' : 'false' }}</code> </li>
</ul>

<p>=>  returns <code>true</code> for all blocks</p>

<ul>
<li><code>{% if block.backgroundColor|length %}</code></li>
</ul>

<p>=> errors out with <code>Craft\MatrixBlockModel and its behaviors do not have a method or closure named "backgroundColor".</code></p>

<ul>
<li><code>{% if block.backgroundColor is defined %}</code></li>
</ul>

<p>=> same error as above</p>

<p>Anyone know how to do this?</p>

CC BY-SA 4.0
2018-06-04T20:16:06.517Z
Checking if the field is defined should work. Is it the missing "d" in this line? {% if block.backgroun**d**Color is defined %}
Simon Kuran
CC BY-SA 4.0
2018-06-04T22:31:07.503Z
Have you tried `{% if block['backgroundColor'] is defined %}` this might not make any sense since it's actually "wrong" (PHP wise)  but Twig has a rare bug in certain cases and doesn't recognize the correct syntax properly
Robin Schambach
CC BY-SA 4.0
2018-06-05T11:39:21.033Z
@RobinSchambach That seems to be working! Could you post it as the answer? :)
Jan Henckens
<templating><matrix><craft3>
Check if a field a included in a Matrix block
# Check if a field a included in a Matrix block

(Asked by: username_0 on 2018-06-04)

Situation: I have a matrix with about 10 block types, 3 of which have a certain field. I want to check if the current block has that field.

The field in question is a dropdown with values in all 3 options.

I've tried:

* `{{ attribute(block, 'backgroundColor') is defined ? 'true' : 'false' }}`

=> returns `true` for all blocks

* `{% if block.backgroundColor|length %}`

=> errors out with `Craft\MatrixBlockModel and its behaviors do not have a method or closure named "backgroundColor".`

* `{% if block.backgroundColor is defined %}`

=> same error as above

Anyone know how to do this?

**Tags:** templating, matrix, craft3

> **Comments:**
> **username_1**: Checking if the field is defined should work. Is it the missing "d" in this line? {% if block.backgroun**d**Color is defined %}  
> **username_2**: Have you tried `{% if block['backgroundColor'] is defined %}` this might not make any sense since it's actually "wrong" (PHP wise)  but Twig has a rare bug in certain cases and doesn't recognize the correct syntax properly  
> **username_0**: @RobinSchambach That seems to be working! Could you post it as the answer? :)  

---

## 1 answers

### Answer by username_2 on 2018-06-05. Score: 4 | Accepted answer

In certain cases Twig is unable to recognize the correct object oriented syntax with magic properties so `{% if object.property is defined %}` will still produce an error.

You can use `{% if block['backgroundColor'] is defined %}` it seems strange and would usually considered as wrong syntax (PHP wise) but it works in Twig


---

