Liquid Truthy and Falsy
In programming, truthy
and falsy
refer to values that return true or false when used in an if
statement.
Every data type must evaluate to either true
or false
. Types that evaluate to true by default are known as truthy. Types that evaluate to false by default are known as falsy.
What is truthy?
All values in Liquid are true, except for nil and false. In this example, the variable is a string type, but it is considered true.
{%- if tobi -%}
This will always be true.
{%- endif -%}
What is falsy?
The only values considered false in Liquid are nil and false. Nil is used when a Liquid object has nothing to return. For instance, if a collection lacks a collection image, collection.image will be set to nil.
<!-- output collection image -->
{%- endif -%}
Value | Truthy | Falsy |
---|---|---|
true | ✓ | |
false | ✓ | |
nil | ✓ | |
empty string | ✓ | |
0 | ✓ | |
integer | ✓ | |
float | ✓ | |
array | ✓ | |
empty array | ✓ | |
page | ✓ | |
empty object | ✓ |
Explanation
In Liquid, only nil and false are false. Be careful when checking values, as they might not be in the expected format but still be true.
For example, empty strings are considered true, so check if they’re empty with blank. EmptyDrop objects are also true, so check if the object is empty.
{{ settings.featured_potions_title }}
{%- else -%}
No value for this setting has been selected.
{%- endif -%}
{%- unless pages.recipes == empty -%}
{{ pages.recipes.content }}
{%- else -%}
No page with this handle exists.
{%- endunless -%}