How to test for empty variables in Liquid
Summary
This article explains how to check for empty variables in Liquid, covering cases such as blank
, empty
, undefined
, and null
. It also provides a complete test to ensure variables contain valid data.
Introduction #
When working with variables in Liquid, you may need to determine if a variable is empty or uninitialized. This article explains different ways to test for empty variables, covering blank
, empty
, undefined
, and null
cases.
Testing for blank
#
The blank
operator checks whether a variable is empty or contains only whitespace. It is useful for handling strings, arrays, and objects.
{% if page.var.blank? %}
Variable is blank.
{% endif %}
A variable is considered blank if it contains:
- An empty string (
""
) - Whitespace-only string (
" "
) - An empty array (
[]
) nil
(Liquid’s equivalent ofnull
)
Testing for empty
#
The empty
operator is used specifically for arrays and hashes. It checks whether a collection contains any elements.
{% if page.array.empty? %}
Array is empty.
{% endif %}
If page.array
has no elements, the condition evaluates to true
.
Testing for undefined
#
A variable that has never been assigned a value is considered undefined
. You can check for an undefined variable using:
{% if page.var %}
Variable is defined.
{% else %}
Variable is undefined.
{% endif %}
If page.var
does not exist, it is treated as false
in a conditional statement.
Testing for null
#
Liquid does not explicitly use null
, but it has nil
, which represents an absence of a value. You can check for nil
using:
{% if page.var == nil %}
Variable is nil.
{% endif %}
nil
behaves similarly to undefined
but can also be assigned explicitly.
Comprehensive test for empty variables #
To ensure that a variable contains a valid value, you should check for all relevant cases:
{% if page.var and page.var != nil and page.var != "" and page.var != empty and page.var != blank %}
Variable is valid.
{% else %}
Variable is empty or undefined.
{% endif %}
This condition ensures that:
page.var
is definedpage.var
is notnil
page.var
is not anempty
stringpage.var
is not an empty collectionpage.var
is notblank
Using this approach, you can reliably check for empty variables in Liquid templates and prevent errors related to missing or invalid data.
Further readings #
Sources and recommended, further resources on the topic:
License
How to test for empty variables in Liquid by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-test-for-empty-variables-in-liquid">How to test for empty variables in Liquid</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the Ditig legal page.