<p>For a relationship field you should use the <a href="http://twig.sensiolabs.org/doc/filters/length.html" rel="nofollow noreferrer">length filter</a>:</p>

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

<p>Or the more efficient <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#outputting-elements" rel="nofollow noreferrer">total function</a> to get and check against the total number of elements that match the criteria:</p>

<pre><code>{% if entry.fieldHandle.total  %}
</code></pre>

<p>A more detailed answer on how to test for empty fieldtypes is here:
<a href="https://craftcms.stackexchange.com/a/842/9">https://craftcms.stackexchange.com/a/842/9</a></p>

<p><em>Note: this answer was edited based on clarification of the question as well as the comments below</em> </p>

CC BY-SA 3.0
2014-07-08T23:47:50.210Z
Hey Ben, at one point I thought the entry variable alone {% if entry.fieldHandle %} was the same as {% if entry.fieldHandle is not empty %} but that doesn't seem to be the case in the way it behaves.  Do you know what the default behavior of {% if entry.fieldHandle %} equates to in Twig?
Ben Parizek
CC BY-SA 3.0
2014-07-09T00:20:08.697Z
i thought i had a grasp on the difference but after reviewing the docs and testing some things i am now unable to find any. i've left a comment for the original question to try to get to the bottom of why the code did not work.
Ben Croker
CC BY-SA 3.0
2014-07-09T16:46:42.980Z
@BenCroker I think a question to investigate the difference between `{% if entry.fieldHandle %}` and `{% if entry.fieldHandle is not empty %}` would be pretty stellar. I'd post it, but then I'd feel vaguely guilty since you Bens brought it up.
Matt Stein
CC BY-SA 3.0
2014-07-09T20:26:19.770Z
i think that would be classified as a twig question rather than craft, and frankly i don't think there is a difference in functionality, maybe use case though.
Ben Croker
CC BY-SA 3.0
2014-07-09T22:28:22.280Z
`{% if entry.fieldHandle %}` is Craft specific and depends on the type of field. See Brandon's [comment on g+](https://plus.google.com/104136663060549664653/posts/VeAQ4QujT6Q).
carlcs
CC BY-SA 3.0
2014-07-09T23:53:08.910Z
@BenCroker Here’s a full breakdown of how to test for whether each field type is empty: http://craftcms.stackexchange.com/a/842/9
Brandon Kelly
CC BY-SA 3.0
2014-07-10T07:44:08.107Z
thanks guys! brandon, are you still considering adding the hasValue function to fieldtypes?
Ben Croker
CC BY-SA 3.0
2014-07-08T22:27:47.937Z
2014-07-10T10:43:47.230Z
2017-04-13T12:51:00.303Z
Ben Croker
<p>The reason it wasn't rendering is because my original entry field is an <em>Entries Field</em> (see <a href="http://buildwithcraft.com/docs/entries-fields#examples" rel="nofollow">templating examples</a> in the docs). The relationship fieldtypes (Entries, Assets, Users) require that you include the <code>length</code> filter in your conditional like so:</p>

<pre><code>{% if entry.fieldHandle |length  %}
  {{ entry.fieldHandle}}
{% else %}
  {{ global.fieldHandle}}
{% endif %}
</code></pre>

<p>Thanks to Keith Mancuso @keithmancuso for <a href="https://gist.github.com/keithmancuso/cc2e933491bb9d7afcc3" rel="nofollow">the fix</a>!</p>

CC BY-SA 3.0
2014-07-09T22:52:02.290Z
I added further insight on why you need to include `length` in your conditional. Hope you didn't mind?! :)
carlcs
CC BY-SA 3.0
2014-07-10T01:06:10.003Z
@ChristianSeelbach Don't mind at all! Thanks for making it more thorough!
lealea
CC BY-SA 3.0
2014-07-10T04:55:14.500Z
`{{ entry.fieldHandle|length ? entry.fieldHandle : global.fieldHandle }}` ... Same code written as a simple [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators).
Lindsey D
CC BY-SA 3.0
2014-07-08T21:48:08.380Z
2014-07-10T01:06:25.367Z
2014-07-10T01:06:25.367Z
lealea
<pre><code>{% if entry.fieldHandle %}
  {{ entry.fieldHandle}}
{% else %}
  {{ global.fieldHandle}}
{% endif %}
</code></pre>

<p>Doesn't seem to be rendering for me. The entry field shows up when it's true, but the global field doesn't in the cases it needs to.</p>

CC BY-SA 3.0
2014-07-09T00:15:34.677Z
Your original code should work, as evaluating an empty array should return false. What type of field is the fieldHandle?
Ben Croker
CC BY-SA 3.0
2014-07-09T22:20:14.790Z
@BenCroker it was an Entries field.
lealea
<templating><conditional><field><entries-field>
How to use a global variable in a conditional statement if an entries relation field is empty?
# How to use a global variable in a conditional statement if an entries relation field is empty?

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

{% if entry.fieldHandle %}
  {{ entry.fieldHandle}}
{% else %}
  {{ global.fieldHandle}}
{% endif %}

Doesn't seem to be rendering for me. The entry field shows up when it's true, but the global field doesn't in the cases it needs to.

**Tags:** templating, conditional, field, entries-field
> **Comments:**
> **username_1**: Your original code should work, as evaluating an empty array should return false. What type of field is the fieldHandle?  
> **username_0**: @BenCroker it was an Entries field.  

---

## 2 answers

### Answer by username_0 on 2014-07-08. Score: 7 | Accepted answer

The reason it wasn't rendering is because my original entry field is an Entries Field (see templating examples in the docs). The relationship fieldtypes (Entries, Assets, Users) require that you include the length filter in your conditional like so:
{% if entry.fieldHandle |length  %}
  {{ entry.fieldHandle}}
{% else %}
  {{ global.fieldHandle}}
{% endif %}

Thanks to Keith Mancuso @keithmancuso for the fix!

> **Comments:**
> **username_4**: I added further insight on why you need to include `length` in your conditional. Hope you didn't mind?! :)  
> **username_0**: @ChristianSeelbach Don't mind at all! Thanks for making it more thorough!  
> **username_6**: `{{ entry.fieldHandle|length ? entry.fieldHandle : global.fieldHandle }}` ... Same code written as a simple [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators).  

---

### Answer by username_1 on 2014-07-08. Score: 2

For a relationship field you should use the length filter:
{% if entry.fieldHandle|length  %}

Or the more efficient total function to get and check against the total number of elements that match the criteria:
{% if entry.fieldHandle.total  %}

A more detailed answer on how to test for empty fieldtypes is here:
https://craftcms.stackexchange.com/a/842/9
Note: this answer was edited based on clarification of the question as well as the comments below

> **Comments:**
> **username_2**: Hey Ben, at one point I thought the entry variable alone {% if entry.fieldHandle %} was the same as {% if entry.fieldHandle is not empty %} but that doesn't seem to be the case in the way it behaves.  Do you know what the default behavior of {% if entry.fieldHandle %} equates to in Twig?  
> **username_1**: i thought i had a grasp on the difference but after reviewing the docs and testing some things i am now unable to find any. i've left a comment for the original question to try to get to the bottom of why the code did not work.  
> **username_3**: @BenCroker I think a question to investigate the difference between `{% if entry.fieldHandle %}` and `{% if entry.fieldHandle is not empty %}` would be pretty stellar. I'd post it, but then I'd feel vaguely guilty since you Bens brought it up.  
> **username_1**: i think that would be classified as a twig question rather than craft, and frankly i don't think there is a difference in functionality, maybe use case though.  
> **username_4**: `{% if entry.fieldHandle %}` is Craft specific and depends on the type of field. See Brandon's [comment on g+](https://plus.google.com/104136663060549664653/posts/VeAQ4QujT6Q).  
> **username_5**: @BenCroker Here’s a full breakdown of how to test for whether each field type is empty: http://craftcms.stackexchange.com/a/842/9  
> **username_1**: thanks guys! brandon, are you still considering adding the hasValue function to fieldtypes?  

---

