<p>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:</p>

<pre><code>{% if entry.modelDataType1 | length %}
    // matrix content here
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-03-16T10:29:01.697Z
2017-03-16T17:42:50.307Z
2017-03-16T17:42:50.307Z
Justin Dekkers
<p>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).  </p>

<p>I can successfully do this for empty simple entries - like this:</p>

<pre><code>{% if entry.modelTitleType %}
    &lt;div class="col-1-1"&gt;
        &lt;h2&gt;{{ entry.modelTitleType }}&lt;/h2&gt;
        &lt;p&gt;{{ entry.modelIntroType }}&lt;/p&gt;
    &lt;/div&gt;
{% endif %}
</code></pre>

<p>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!</p>

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

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

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

<pre><code>{% if entry.modelDataType1 %}
</code></pre>

<p>But this doesn't work...</p>

<p>Any help would be very much appreciated!</p>

<p>Many thanks.</p>

<matrix>
How do you use 'if' to stop an empty Matrix block from displaying?
# How do you use 'if' to stop an empty Matrix block from displaying?

(Asked by: username_0 on 2017-03-16)

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 %}
    
        {{ entry.modelTitleType }}
        {{ entry.modelIntroType }}
    
{% 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?

    {% for block in entry.modelDataType1 %}
        {% if block.type == "type" %}
            {{ block.typeName }}
        {% endif %}
    {% endfor %}
    {% for block in entry.modelDataType2 %}
        {% if block.type == "type" %}
            {{ block.typeName }}
        {% endif %}
    {% endfor %}


I've tried:
{% if entry.modelDataType1 %}

But this doesn't work...
Any help would be very much appreciated!
Many thanks.

**Tags:** matrix

---

## 1 answers

### Answer by username_1 on 2017-03-16. Score: 1 | Accepted answer

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


---

