Liquid objects can have one of six types. You can initialize Liquid variables with the assign or capture tags.
String
Strings are declared by wrapping the value of a variable 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 quotes are needed when you declare a Boolean.
{% assign foo = true %}
{% assign bar = false %}
Nil
Nil is a special empty value that is returned when Liquid code has no result. It is not a string with the characters "nil".
Nil counts as false in the conditions of if blocks and other Liquid tags that check whether a statement is true.
In this example, Liquid does not output the text if there is no tracking number (fulfillment.tracking_numbers returns nil):
{% if fulfillment.tracking_numbers %}
There is a tracking number.
{% endif %}
Tags or outputs that return nil print nothing on 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 items in an array, loop through it with a for or tablerow tag.
{% for tag in product.tags %}
{{ tag }}
{% endfor %}
sale summer spring wholesale
Accessing specific items in arrays
Use square brackets [ ] to access a specific item in an array. Array indexes start at zero.
<!-- if product.tags = "sale", "spring", "summer", "wholesale" -->
{{ product.tags[0] }}
{{ product.tags[1] }}
{{ product.tags[2] }}
sale
spring
summer
Initializing arrays
You cannot initialize arrays with Liquid alone.
You can, however, use the split filter to break a single string into an array of substrings.
EmptyDrop
An EmptyDrop object is returned when you try to access a deleted object (for example a page or a post) by its handle. In this example, 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 whether an object exists before you access one of its attributes.
{% unless pages.about == empty %}
<!-- Only printed if the page with the handle "about" is not empty -->
<h1>{{ pages.about.title }}</h1>
<div>
{{ pages.about.content }}
</div>
{% endunless %}
If you do not check for emptiness first, Liquid can output empty HTML elements:
<h1></h1>
<div></div>
You can check collections for emptiness, too:
{% 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 have only one attribute, empty?, which is always true.
Existing collections and pages have no empty? attribute. Their empty? is falsy, so calling it inside an if statement returns false. In an unless statement on existing collections and pages, empty? returns true.
To check whether a drop is empty, do not use empty?. Use a condition that compares the drop with empty.