On a complex page, where there are different sections that need to be present in the template, but may not all have content added via Craft entries I need to stop the empty areas from being output (e.g. a set of div's that will create an ugly empty space in the page).

I can successfully do this for empty simple entries - like this:

{% if entry.modelTitleType %}
    <div class="col-1-1">
        <h2>{{ entry.modelTitleType }}</h2>
        <p>{{ entry.modelIntroType }}</p>
{% endif %}

So the div enclosed by the 'if' statement is only output if there's an entry added for 'modelTitleType'. But I cannot get this to work for a Matrix entry!

How do I wrap an 'if' statement around a Matrix block like this, so it is only output if there's data added?

<ul class="selector padded-top inline-list">
    {% for block in entry.modelDataType1 %}
        {% if block.type == "type" %}
            <li><a class="button-list selected" id="show-type1" title="{{ block.typeName }}">{{ block.typeName }}</a></li>
        {% endif %}
    {% endfor %}
    {% for block in entry.modelDataType2 %}
        {% if block.type == "type" %}
            <li><a class="button-list" id="show-type2" title="{{ block.typeName }}">{{ block.typeName }}</a></li>
        {% endif %}
    {% endfor %}

I've tried:

{% if entry.modelDataType1 %}

But this doesn't work...

Any help would be very much appreciated!

Many thanks.

1 Answer 1


If a Matrix field / block is empty it won't output at all but you can try checking the length in an if statement like:

{% if entry.modelDataType1 | length %}
    // matrix content here
{% endif %}

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.