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

                    <p>{{ block.quote }}</p>
                    <cite>– {{ block.attribution }}</cite>

            {% 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!

  • 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
    Commented May 6, 2015 at 15:49
  • 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: craftcms.stackexchange.com/questions/5482/…
    – Jamie Wade
    Commented May 6, 2015 at 15:56
  • 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
    Commented May 6, 2015 at 16:04
  • 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
    Commented May 6, 2015 at 16:07
  • 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
    Commented May 6, 2015 at 16:15

2 Answers 2


Use brackets instead of dot notation for the field handle when you test for defined:

{% if block['test'] is defined %}
{% endif %}
  • Thanks, this fixes the issue for the time being. I really appreciate the help!
    – user1204
    Commented May 7, 2015 at 1:22
  • No problem – happy it worked for you, @user1204 :) Commented May 7, 2015 at 5:06

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.