I have read docs on Craft CMS site and Googled some related articles but can't seem to get my head around checking for conditions for a field.

For example, if I have a 'Fruit' field drop-down: Apple Banana Orange

Should I add a 4th value with 'null'? So in my template, I want to display the field if it is Apple, Banana or Orange but not if null.

I get confused when to to use '|length' or 'is empty' or 'is not' conditionals.

In another example, I tried to setup a drop-down for a field called 'Rating' The values were 1, 2, 3, 4, 5 I couldn't get the conditional to work, so I thought to make the field a number type. And then I tried a conditional 'rating > 0' but also didn't succeed.


If you want a Dropdown field with the option to not select a value (or to clear out the current selection), you can just add an option (typically the first) with an empty value column. If the empty value/option is selected, the following conditional will evaluate to false, and if any other, non-empty value is selected it will evaluate to true:

{% if entry.dropdownFieldHandle %}
    I have a value: {{ entry.dropdownFieldHandle }}
{% else %}
    I have no value in the dropdown
{% endif %}

Note that for Dropdown fields its also possible to leave the label column empty, or to use something like "Pick a value..." as a placeholder.

If you use null instead of an empty value, the above conditional will always evaluate to true, because Twig will treat the value null as a string (i.e. "null"), not an actual NULL value.

As for the |length filter, since your question brings it up – the only thing |length does is to return the length of whatever variable its suffixed to. For an iterable object (like an array or a relational field like Assets or Entries) the length will be the amount of elements in that object (for instance, the number of Assets selected in an Assets field); for strings and numbers it will be the number of characters (meaning "foo"|length and 123|length will both return 3).

The reason |length is often used in conditionals, is that 0 is considered a "falsey" value, and any length larger than 0 is considered a "truthy" value. In other words, a conditional like {% if ""|length %} will be evaluated to false, because "" is an empty string and hence will return 0 characters, while a conditional like {% if "foo"|length %} will evaluate to true because the length filter will return 3 characters and 3 is a "truthy" value.

That being said, the reason why the |length filter isn't neccessary in the above example conditional is simply because Twig will automatically evaluate conditionals testing empty strings to false. So while {% if entry.dropdownFieldHandle|length %} would also work (i.e. evaluate to false for empty values), it isn't actually neccessary.

Finally, is not (or simply not) is simply Twig-speak for the != operator, i.e. is not equal to. So if you wanted to reverse the conditional above, here's how that would look:

{% if not entry.dropdownFieldHandle %}
    I have no value in the dropdown
{% else %}
    I have a value: {{ entry.dropdownFieldHandle }}
{% endif %}
  • I changed the first value to blank (no data) and tried: {% if entry.GamesGraphicsMode %}Graphics Mode: {{ entry.GamesGraphicsMode }}{% endif %} But the result is that 'Graphics Mode:' is shown regardless of whether I have the drop-down selected to the first option (blank) or an option with data. – DevServe Dec 21 '16 at 3:10
  • 1
    Is the option value really empty, or did you accidentally make it a blank space or something like that? Also, note that Dropdown fields are not dynamic – i.e. if you change the GamesGraphicsMode field's options (e.g. by changing an option value from "null" to an empty value), any existing values for your entries won't update; you'll need to resave existing entries. You can always try to {{ dump(entry. GamesGraphicsMode) }} and see what it contains, because I guarantee that it isn't empty :) (the dump function requires devMode to be on) – Mats Mikkel Rummelhoff Dec 21 '16 at 6:52
  • 1
    I got it to work. I set the first Label to "Select" and option to "select", in my template, I used: {% if entry.GamesGraphicsMode != "select" %} – DevServe Dec 22 '16 at 0:38
  • Sure, that'll work. I'm still pretty sure there was some junk data (a blank space, invisible characters, etc) in your previous "empty" value cell though, because leaving the value empty and simply doing {% if entry.GamesGraphicsMode %} should work, too (I've done it like that many times). Anyway, it's that you got it working :) – Mats Mikkel Rummelhoff Dec 22 '16 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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