<p>If you have a peek through the <a href="http://twig.sensiolabs.org/documentation">Twig documentation</a>, you'll come across the <a href="http://twig.sensiolabs.org/doc/tests/empty.html"><code>empty</code></a> test.</p>

<pre><code>{% if block.heading is empty %}
    {# ... #}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-07-08T15:19:51.127Z
2014-07-08T15:19:51.127Z
Stuart Whitehead
<p>Stuart's answer totally works. You can also use Twig's <a href="http://twig.sensiolabs.org/doc/filters/length.html"><code>length</code></a> filter:</p>

<pre><code>{% if article.articleImage|length %}
</code></pre>

CC BY-SA 3.0
2014-07-08T18:48:07.367Z
2014-07-08T18:48:07.367Z
Simon Kuran
<p>Well this documentation helped me a lot to check any conditions. It might help others too.</p>

<p><a href="https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig" rel="nofollow noreferrer">https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig</a></p>

CC BY-SA 3.0
2017-08-01T04:56:22.320Z
2017-08-01T04:56:22.320Z
hashtagerrors
<p>The answer depends on the field type.</p>

<p>Assets, Categories, Checkboxes, Dropdown, Entries, Matrix, Multi-select, Radio Buttons, Tags, and Users fields each will each appear to be non-empty, even if they don’t have a user-submitted value, because in each case, <code>entry.myFieldHandle</code> will still return <em>something</em>:</p>

<ul>
<li>an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#outputting-elements"><strong>ElementCriteriaModel</strong></a> object for Assets, Categories, Entries, Matrix, Tags, and Users fields</li>
<li>a <strong>MultiOptionsFieldData</strong> object for Checkboxes and Multi-select fields</li>
<li>a <strong>SingleOptionFieldData</strong> object for Dropdown and Radio Buttons fields</li>
</ul>

<p>So for each of those field types, you will need to use the <a href="http://twig.sensiolabs.org/doc/filters/length.html"><code>|length</code></a> filter to determine if there is an actual value.</p>

<pre><code>{% if not entry.myCheckboxesField|length %}
</code></pre>

<p>Also worth noting for Assets, Categories, Entries, Matrix, Tags, and Users fields: ElementCriteriaModel objects also have a <code>total()</code> function which can be used to speed up your template if all you need to know is whether any elements were selected, and you won’t be actually looping through them further down in the template. <em>(<code>|length</code> actually fetches the elements first, and then returns the count of the results, but stores those elements internally in case they are going to be looped through later on. <code>.total()</code> runs a separate, much less expensive DB query, but the results won’t be cached internally for later use.)</em></p>

<pre><code>{% if not entry.myCategoriesField.total() %}
</code></pre>

<p>Color, Date/Time, Lightswitch, Number, Plain Text, Rich Text, and Table fields will each return an empty value (<code>null</code>, an empty string/array, etc.) when there is no value, so each of those field types are safe to use with the <a href="http://twig.sensiolabs.org/doc/tests/empty.html"><code>is empty</code></a> test:</p>

<pre><code>{% if entry.myTextField is empty %}
</code></pre>

<p>This would also work:</p>

<pre><code>{% if not entry.myTextField %}
</code></pre>

<p>In all cases, the <code>|length</code> filter will work as expected, so if you don’t want to remember this stuff, just use that.</p>

CC BY-SA 3.0
2014-07-08T21:03:54.087Z
2014-07-08T21:59:50.970Z
2014-07-08T21:59:50.970Z
Brandon Kelly
<p>How do I check if a field is blank? I thought it was something like </p>

<pre><code>{% if article.articleImage is defined %}
</code></pre>

<p>Which in this case is testing an Assets field (and seems to work). Tried the same for a text field (in a Matrix row), and that doesn't work, as far as I can tell:</p>

<pre><code>{{ if block.heading is defined }}
</code></pre>

CC BY-SA 3.0
2014-07-08T16:13:26.373Z
FYI, [there's a nice Q&A](http://craftcms.stackexchange.com/questions/356/whats-the-difference-between-is-defined-and-is-not-null/360#360) on the difference between `is defined` and `is not null`.
Matt Stein
<templating><matrix>
Test for empty field?
# Test for empty field?

(Asked by: username_0 on 2014-07-08)

How do I check if a field is blank? I thought it was something like 
{% if article.articleImage is defined %}

Which in this case is testing an Assets field (and seems to work). Tried the same for a text field (in a Matrix row), and that doesn't work, as far as I can tell:
{{ if block.heading is defined }}

**Tags:** templating, matrix
> **Comments:**
> **username_1**: FYI, [there's a nice Q&A](http://craftcms.stackexchange.com/questions/356/whats-the-difference-between-is-defined-and-is-not-null/360#360) on the difference between `is defined` and `is not null`.  

---

## 4 answers

### Answer by username_5 on 2014-07-08. Score: 54 | Accepted answer

The answer depends on the field type.
Assets, Categories, Checkboxes, Dropdown, Entries, Matrix, Multi-select, Radio Buttons, Tags, and Users fields each will each appear to be non-empty, even if they don’t have a user-submitted value, because in each case, entry.myFieldHandle will still return something:

an ElementCriteriaModel object for Assets, Categories, Entries, Matrix, Tags, and Users fields
a MultiOptionsFieldData object for Checkboxes and Multi-select fields
a SingleOptionFieldData object for Dropdown and Radio Buttons fields

So for each of those field types, you will need to use the |length filter to determine if there is an actual value.
{% if not entry.myCheckboxesField|length %}

Also worth noting for Assets, Categories, Entries, Matrix, Tags, and Users fields: ElementCriteriaModel objects also have a total() function which can be used to speed up your template if all you need to know is whether any elements were selected, and you won’t be actually looping through them further down in the template. (|length actually fetches the elements first, and then returns the count of the results, but stores those elements internally in case they are going to be looped through later on. .total() runs a separate, much less expensive DB query, but the results won’t be cached internally for later use.)
{% if not entry.myCategoriesField.total() %}

Color, Date/Time, Lightswitch, Number, Plain Text, Rich Text, and Table fields will each return an empty value (null, an empty string/array, etc.) when there is no value, so each of those field types are safe to use with the is empty test:
{% if entry.myTextField is empty %}

This would also work:
{% if not entry.myTextField %}

In all cases, the |length filter will work as expected, so if you don’t want to remember this stuff, just use that.


---

### Answer by username_2 on 2014-07-08. Score: 12

If you have a peek through the Twig documentation, you'll come across the empty test.
{% if block.heading is empty %}
    {# ... #}
{% endif %}


---

### Answer by username_3 on 2014-07-08. Score: 7

Stuart's answer totally works. You can also use Twig's length filter:
{% if article.articleImage|length %}


---

### Answer by username_4 on 2017-08-01. Score: 1

Well this documentation helped me a lot to check any conditions. It might help others too.

<https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig>


---

