In programming, we describe “truthy” and “falsy” as anything that returns true or false, respectively, when used inside an if statement.
What is truthy?
All values in Liquid are truthy, with the exception of nil and false.
In the example below, the text “Tobi” is not a boolean, but it is truthy in a conditional:
{% assign tobi = 'Tobi' %} {% if tobi %} This will always be true. {% endif %}
Strings, even when empty, are truthy. The example below will result in empty HTML tags ifsettings.fp_heading is empty:
settings.fp_heading
Input
{% if settings.fp_heading %} <h1>{{ settings.fp_heading }}</h1> {% endif %}
Output
<h1></h1>
To avoid this, you can check to see if the string is blank, as follows:
blank
{% unless settings.fp_heading == blank %} <h1>{{ settings.fp_heading }}</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
settings.page
{% if pages[settings.page] %} <div>{{ pages[settings.page].content }}</div> {% endif %}
<div></div>
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.
The table below summarizes what is truthy or falsy in Liquid.
Was this article helpfu?
Thank you for voting
You are related to multiple companies. Please select the company you wish to login as.