Data types
Liquid objects can be one of six types. You can initialize Liquid variables using assign or capture tags.
String
Strings are declared by wrapping a variable's value in single or double quotes.
{% assign my_string = "Hello World!" %}
Number
Numbers include floats and integers.
{% assign my_int = 25 %}
{% assign my_float = 39.756 %}
Boolean
Booleans are either true
or false
. No quotations are necessary when declaring a boolean.
{% assign foo = true %}
{% assign bar = false %}
Nil
Nil is a special empty value that is returned when Liquid code has no results. It is not a string with the characters "nil".
Nil is treated as false in the conditions of if
blocks and other Liquid tags that check the truthfulness of a statement.
In the following example, if a tracking number does not exist (that is, fulfillment.tracking_numbers
returns nil
), Liquid will not print the text:
{% if fulfillment.tracking_numbers %}
There is a tracking number.
{% endif %}
Tags or outputs that return nil
will not print anything to the page.
Tracking number: {{ fulfillment.tracking_numbers }}
Tracking number:
Array
Arrays hold lists of variables of any type.
Accessing items in arrays
To access all of the items in an array, you can loop through each item in the array using a for or tablerow tag.
{% for tag in product.tags %}
{{ tag }}
{% endfor %}
sale summer spring wholesale
Accessing specific items in arrays
You can use square bracket [
]
notation to access a specific item in an array. Array indexing starts at zero.
<!-- if product.tags = "sale", "spring", "summer", "wholesale" -->
{{ product.tag[0] }}
{{ product.tag[1] }}
{{ product.tag[2] }}
sale
spring
summer
Initializing arrays
You cannot initialize arrays using only Liquid.
You can, however, use the split filter to break a single string into an array of substrings.
EmptyDrop
An EmptyDrop object is returned if you try to access a deleted object (such as a page or post) by its handle. In the example below, page_1
, page_2
and page_3
are all EmptyDrop objects.
{% assign variable = "hello" %}
{% assign page_1 = pages[variable] %}
{% assign page_2 = pages["does-not-exist"] %}
{% assign page_3 = pages.this-handle-does-not-exist %}
Checking for emptiness
You can check to see if an object exists or not before you access any of its attributes.
{% unless pages == empty %}
<!-- This will only print if the page with handle "about" is not empty -->
<h1>{{ pages.frontpage.title }}</h1>
<div>{{ pages.frontpage.content }}</div>
{% endunless %}
If you don't check for emptiness first, Liquid might print empty HTML elements:
<h1></h1>
<div></div>
You can check for emptiness with collections as well:
{% unless collections.frontpage == empty %}
{% for product in collections.frontpage.products %}
{% include "product-grid-item" %}
{% else %}
<p>We have a "frontpage" collection, but it's empty.</p>
{% endfor %}
{% endunless %}
EmptyDrop attributes
EmptyDrop objects only have one attribute, empty?
, which is always true.
Collections and pages that do exist do not have an empty?
attribute. Their empty?
is “falsy”, which means that calling it inside an if
statement will return false. When using an unless
statement on existing collections and pages, empty?
will return true
.
If you need to check if a drop is empty, rather than using the empty?
attribute, use a conditional statement to check whether the drop is equal to empty
.