Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.
{% 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.

share|improve this question
    
Your original code should work, as evaluating an empty array should return false. What type of field is the fieldHandle? –  Ben Croker Jul 9 at 0:15
    
@BenCroker it was an Entries field. –  lealea Jul 9 at 22:20

2 Answers 2

up vote 6 down vote accepted

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!

share|improve this answer
1  
I added further insight on why you need to include length in your conditional. Hope you didn't mind?! :) –  carlcs Jul 9 at 22:52
    
@ChristianSeelbach Don't mind at all! Thanks for making it more thorough! –  lealea Jul 10 at 1:06
    
{{ entry.fieldHandle|length ? entry.fieldHandle : global.fieldHandle }} ... Same code written as a simple ternary operator. –  Lindsey D Jul 10 at 4:55

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: http://craftcms.stackexchange.com/a/842/9

Note: this answer was edited based on clarification of the question as well as the comments below

share|improve this answer
    
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 Jul 8 at 23:47
    
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 Jul 9 at 0:20
    
@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 Jul 9 at 16:46
1  
{% if entry.fieldHandle %} is Craft specific and depends on the type of field. See Brandon's comment on g+. –  carlcs Jul 9 at 22:28
2  
@BenCroker Here’s a full breakdown of how to test for whether each field type is empty: craftcms.stackexchange.com/a/842/9 –  Brandon Kelly Jul 9 at 23:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.