Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T23:47:50.210Z
Answers.Text: 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?
Answers.UserDisplayName: Ben Parizek
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T00:20:08.697Z
Answers.Text: 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.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T16:46:42.980Z
Answers.Text: @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.
Answers.UserDisplayName: Matt Stein
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T20:26:19.770Z
Answers.Text: 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.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T22:28:22.280Z
Answers.Text: `{% 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).
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T23:53:08.910Z
Answers.Text: @BenCroker Here’s a full breakdown of how to test for whether each field type is empty: http://craftcms.stackexchange.com/a/842/9
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-10T07:44:08.107Z
Answers.Text: thanks guys! brandon, are you still considering adding the hasValue function to fieldtypes?
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T22:27:47.937Z
Answers.LastActivityDate: 2014-07-10T10:43:47.230Z
Answers.LastEditDate: 2017-04-13T12:51:00.303Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T22:52:02.290Z
Answers.Text: I added further insight on why you need to include `length` in your conditional. Hope you didn't mind?! :)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-10T01:06:10.003Z
Answers.Text: @ChristianSeelbach Don't mind at all! Thanks for making it more thorough!
Answers.UserDisplayName: lealea
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-10T04:55:14.500Z
Answers.Text: `{{ entry.fieldHandle|length ? entry.fieldHandle : global.fieldHandle }}` ... Same code written as a simple [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators).
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T21:48:08.380Z
Answers.LastActivityDate: 2014-07-10T01:06:25.367Z
Answers.LastEditDate: 2014-07-10T01:06:25.367Z
Answers.UserDisplayName: lealea
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-09T00:15:34.677Z
Comments.Text: Your original code should work, as evaluating an empty array should return false. What type of field is the fieldHandle?
Comments.UserDisplayName: Ben Croker
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-09T22:20:14.790Z
Comments.Text: @BenCroker it was an Entries field.
Comments.UserDisplayName: lealea
Tags: <templating><conditional><field><entries-field>
Title: How to use a global variable in a conditional statement if an entries relation field is empty?
ThreadText: # 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**: @username_1 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](http://buildwithcraft.com/docs/entries-fields#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](https://gist.github.com/keithmancuso/cc2e933491bb9d7afcc3)!
  
  > **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](http://twig.sensiolabs.org/doc/filters/length.html):
  
  ```
  {% if entry.fieldHandle|length  %}
  
  ```
  
  Or the more efficient [total function](http://buildwithcraft.com/docs/templating/elementcriteriamodel#outputting-elements) 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**: @username_1 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 username_5's [comment on g+](https://plus.google.com/104136663060549664653/posts/VeAQ4QujT6Q).  
  > **username_5**: @username_1 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! username_5, are you still considering adding the hasValue function to fieldtypes?  
  
  ---
  
  