<p>You can probably get away with removing <code>|length</code> for most conditionals that check for the existence of content in a custom field and just check on <code>entry.fieldHandle</code> instead.</p>

<p>One exception to this would be for lightswitch fields, which always return <code>true</code> for a <code>|length</code> check, but return <code>true</code> or <code>false</code> when checking on <code>entry.fieldHandle</code> depending on whether the lightswitch is <code>on</code> or <code>off</code>.</p>

<p>StraightUpCraft has a great article on <a href="https://straightupcraft.com/articles/testing-if-something-exists-is-defined-length-is-not-null-is-not-empty" rel="nofollow noreferrer">Testing if something exists</a> and another on <a href="https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig" rel="nofollow noreferrer">How to check if a variable or value exists using Twig</a>, which cover a multitude of cases and ways of accessing variables using Twig.</p>

CC BY-SA 3.0
2016-11-22T12:38:32.397Z
2016-11-22T12:38:32.397Z
Steve Rowling
<p>When checking if a field has content or not in my templates I usually write something along the lines of:</p>

<pre><code>{% if entry.fieldHandle|length %}
    &lt;h1&gt;{{ entry.fieldHandle }}&lt;/h1&gt; 
{% endif %}
</code></pre>

<p>Writing the same code without the <code>|length</code> filter does also work, so can I just exclude the use of the filter from my code altogether? Are there any caveats with <strong><em>not</em></strong> using the filter?</p>

<p>As an aside, I am aware that <code>|length</code> does have its uses. You can use this to check the exact value of something, etc etc.</p>

<p>This question was more suited for a simple yes/no check to see if a field has any content and outputting something if it does.</p>

<p>Another example of the <code>|length</code> filter in my code would be something like:</p>

<pre><code>{% set homepageServices = craft.entries.section('services').type('serviceGroup').level(1) %}
{% if homepageServices|length %}
    &lt;p&gt;Output the services here&lt;/p&gt;
{% endif %}
</code></pre>

<p>Would removing <code>|length</code> (which does work at the moment) cause me any headaches down the line?</p>

<templating><conditional>
Is the length filter required when checking if a field has content or not?
# Is the length filter required when checking if a field has content or not?

(Asked by: username_0 on 2016-11-22)

When checking if a field has content or not in my templates I usually write something along the lines of:
{% if entry.fieldHandle|length %}
    {{ entry.fieldHandle }} 
{% endif %}

Writing the same code without the |length filter does also work, so can I just exclude the use of the filter from my code altogether? Are there any caveats with not using the filter?
As an aside, I am aware that |length does have its uses. You can use this to check the exact value of something, etc etc.
This question was more suited for a simple yes/no check to see if a field has any content and outputting something if it does.
Another example of the |length filter in my code would be something like:
{% set homepageServices = craft.entries.section('services').type('serviceGroup').level(1) %}
{% if homepageServices|length %}
    Output the services here
{% endif %}

Would removing |length (which does work at the moment) cause me any headaches down the line?

**Tags:** templating, conditional


---

## 1 answers

### Answer by username_1 on 2016-11-22. Score: 4 | Accepted answer

You can probably get away with removing `|length` for most conditionals that check for the existence of content in a custom field and just check on `entry.fieldHandle` instead.

One exception to this would be for lightswitch fields, which always return `true` for a `|length` check, but return `true` or `false` when checking on `entry.fieldHandle` depending on whether the lightswitch is `on` or `off`.

StraightUpCraft has a great article on [Testing if something exists](https://straightupcraft.com/articles/testing-if-something-exists-is-defined-length-is-not-null-is-not-empty) and another on [How to check if a variable or value exists using Twig](https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig), which cover a multitude of cases and ways of accessing variables using Twig.


---

