5
{% 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.

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

2 Answers 2

7

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!

3
  • 1
    I added further insight on why you need to include length in your conditional. Hope you didn't mind?! :)
    – carlcs
    Commented Jul 9, 2014 at 22:52
  • @ChristianSeelbach Don't mind at all! Thanks for making it more thorough!
    – lealea
    Commented Jul 10, 2014 at 1:06
  • {{ entry.fieldHandle|length ? entry.fieldHandle : global.fieldHandle }} ... Same code written as a simple ternary operator.
    – Lindsey D
    Commented Jul 10, 2014 at 4:55
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

7
  • 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? Commented Jul 8, 2014 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
    Commented Jul 9, 2014 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
    Commented Jul 9, 2014 at 16:46
  • 1
    {% if entry.fieldHandle %} is Craft specific and depends on the type of field. See Brandon's comment on g+.
    – carlcs
    Commented Jul 9, 2014 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 Commented Jul 9, 2014 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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