Skip to main content
Liquid Syntax:

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:

  1. An empty string ("")
  2. Whitespace-only string (" ")
  3. An empty array ([])
  4. nil (Liquid’s equivalent of null)

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:

  1. page.var is defined
  2. page.var is not nil
  3. page.var is not an empty string
  4. page.var is not an empty collection
  5. page.var is not blank

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:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

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.

All Topics

Random Quote

“Content precedes design. Design in the absence of content is not design, it's decoration.”

 Jeffery Zeldman American web designer, author, and advocate for web standardsTwitter, - IT quotes