<p>It depends. Technically to determine if a variable is not empty (keep in mind '0' or 'false' are not considered empty), you can use Twig's <a href="http://twig.sensiolabs.org/doc/tests/empty.html#empty" rel="nofollow">'is not empty'</a>:</p>

<pre><code>{% if entry.myField is not empty %}...{% endif %}
</code></pre>

<p>But in most situations where '0' and/or 'false' are not expected values, then  anything that evaluates to '0', 'false' or 'null' will also work. So,</p>

<pre><code>{% if entry.myField %}...{% endif %} 
</code></pre>

<p>or </p>

<pre><code>{% if entry.myField|length %}...{% endif %}
</code></pre>

<p>If the field is not defined in the field layout however, these may also return an error. In which case you might want to use Twig's <a href="http://twig.sensiolabs.org/doc/tests/defined.html#defined" rel="nofollow">'is defined'</a> first, before checking if the field is empty.</p>

<pre><code>`{% if entry.myField is defined %}...{% endif %}`
</code></pre>

<p>In most cases, I just use <code>{% if entry.myField %}...{% endif %}</code>.</p>

CC BY-SA 3.0
2015-05-07T07:21:32.323Z
Not really related to the question but maybe this helps someone reading this great answer. You can get only those entries with non empty fields using a custom field parameter on the ElementCriteriaModel like so: `{% set entries = craft.entries.myField(':notempty:') %}`.
carlcs
CC BY-SA 3.0
2015-05-07T03:33:29.927Z
2015-05-07T07:10:39.407Z
2015-05-07T07:10:39.407Z
Douglas McDonald
<p>It depends on the field type. If it is an entry (or asset or user), then <code>entry.myentry</code> will be an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a>, which even if there are no elements will always be 'truthy'. In that case, you need <code>entry.myentry|length</code>.</p>

<p>Otherwise, depending on what you want to do about the field being <code>0</code> or <code>false</code> you may want one of:</p>

<ul>
<li><code>entry.myentry</code> : true/false determined as in php. <a href="http://twig.sensiolabs.org/doc/tags/if.html" rel="nofollow">twig if documentation explains this.</a></li>
<li><code>entry.myentry is not empty</code> - false for <code>null</code>, <code>false</code>, empty array or empty string. (<a href="http://twig.sensiolabs.org/doc/tests/empty.html" rel="nofollow">empty</a>)</li>
<li><code>entry.myentry is not null</code> - false for <code>null</code>. (<a href="http://twig.sensiolabs.org/doc/tests/null.html" rel="nofollow">null</a>)</li>
<li><code>entry.myentry|length</code> - this will be <code>0</code> if entry.myentry is <code>false</code> or <code>null</code> and <code>1</code> if entry.myentry is <code>0</code>. (<a href="http://twig.sensiolabs.org/doc/filters/length.html" rel="nofollow">length</a>)</li>
</ul>

<p>I personally would <code>{{ dump(entry.myentry) }}</code> for one of your "empty" entries to get more clarity on which of these would be best.</p>

CC BY-SA 3.0
2015-05-07T22:21:23.923Z
2015-05-07T22:21:23.923Z
Marion Newlevant
<p>What's the correct way to create a conditional so that a field won't display if it's empty? Is it:</p>

<pre><code>{% if entry.myentry | length %}
    ...
{% endif %} 
</code></pre>

<p>or </p>

<pre><code>{% if entry.myentry %}
    ...
{% endif %}
</code></pre>

<p>I tried both and both seem to work, but I want to make sure they're both correct.</p>

CC BY-SA 3.0
2015-05-07T03:50:29.707Z
Not a dupe, but related: http://craftcms.stackexchange.com/q/356/45
Lindsey D
<conditional>
Correct code for a conditional field?
# Correct code for a conditional field?

(Asked by: username_0 on 2015-05-07)

What's the correct way to create a conditional so that a field won't display if it's empty? Is it:
{% if entry.myentry | length %}
    ...
{% endif %} 

or 
{% if entry.myentry %}
    ...
{% endif %}

I tried both and both seem to work, but I want to make sure they're both correct.

**Tags:** conditional
> **Comments:**
> **username_1**: Not a dupe, but related: http://craftcms.stackexchange.com/q/356/45  

---

## 2 answers

### Answer by username_4 on 2015-05-07. Score: 4 | Accepted answer

It depends on the field type. If it is an entry (or asset or user), then `entry.myentry` will be an [ElementCriteriaModel](http://buildwithcraft.com/docs/templating/elementcriteriamodel), which even if there are no elements will always be 'truthy'. In that case, you need `entry.myentry|length`.

Otherwise, depending on what you want to do about the field being `0` or `false` you may want one of:

* `entry.myentry` : true/false determined as in php. [twig if documentation explains this.](http://twig.sensiolabs.org/doc/tags/if.html)
* `entry.myentry is not empty` - false for `null`, `false`, empty array or empty string. ([empty](http://twig.sensiolabs.org/doc/tests/empty.html))
* `entry.myentry is not null` - false for `null`. ([null](http://twig.sensiolabs.org/doc/tests/null.html))
* `entry.myentry|length` - this will be `0` if entry.myentry is `false` or `null` and `1` if entry.myentry is `0`. ([length](http://twig.sensiolabs.org/doc/filters/length.html))

I personally would `{{ dump(entry.myentry) }}` for one of your "empty" entries to get more clarity on which of these would be best.


---

### Answer by username_2 on 2015-05-07. Score: 8

It depends. Technically to determine if a variable is not empty (keep in mind '0' or 'false' are not considered empty), you can use Twig's 'is not empty':
{% if entry.myField is not empty %}...{% endif %}

But in most situations where '0' and/or 'false' are not expected values, then  anything that evaluates to '0', 'false' or 'null' will also work. So,
{% if entry.myField %}...{% endif %} 

or 
{% if entry.myField|length %}...{% endif %}

If the field is not defined in the field layout however, these may also return an error. In which case you might want to use Twig's 'is defined' first, before checking if the field is empty.
`{% if entry.myField is defined %}...{% endif %}`

In most cases, I just use {% if entry.myField %}...{% endif %}.

> **Comments:**
> **username_3**: Not really related to the question but maybe this helps someone reading this great answer. You can get only those entries with non empty fields using a custom field parameter on the ElementCriteriaModel like so: `{% set entries = craft.entries.myField(':notempty:') %}`.  

---

