Truthiness and falsiness in Liquid
When a non-boolean data type is used in a boolean context (such as a conditional tag), Liquid decides whether to evaluate it as true or false. Data types that return true by default are called truthy. Data types that return false by default are called falsy.
In this article
Truthy
All values in Liquid are truthy except nil and false.
In the example below, the text “Tobi” is not a boolean, but it is truthy in a conditional:
{% assign name = "Tobi" %}
{% if name == true %}
This text will always appear if "name" is defined.
{% endif %}
Empty strings are truthy. The example below will create empty HTML tags if current_page.title exists but is empty:
{% if current_page.title %}
<h1>{{ current_page.title }}</h1>
{% endif %}
Output
<h1></h1>
To avoid this, you can check to see if the string is blank, as follows:
{% unless current_page.title == blank %}
<h1>{{ current_page.title }}</h1>
{% endunless %}
An EmptyDrop is also truthy. In the example below, if settings.page is an empty string or set to a hidden or deleted object, you will end up with an EmptyDrop. The result is an undesirable empty <div>.
Input
{% if pages[settings.page] %}
<div>{{ pages[settings.page].content }}</div>
{% endif %}
Output
<div></div>
Falsy
The only values that are falsy in Liquid are nil and false.
nil is returned when a Liquid object doesn’t have anything to return. For example, if a collection doesn’t have a collection image, collection.image will be set to ``nil`. Since that is “falsy”, you can do this:
{% if collection.image %}
<!-- output collection image -->
{% endif %}
The value false is returned through many Liquid object properties such as product.available.