We are here and ready to help.

Types

Liquid objects can return one of six types: String, Number, Boolean, Nil, Array, or EmptyDrop. Liquid variables can be initialized by using the assign or capture tags.

Strings

Strings are declared by wrapping the variable's value in single or double quotes.

{% assign my_string = "Hello World!" %}

 

Numbers

Numbers include floats and integers.

{% assign my_num = 25 %}

 

Booleans

Booleans are either true or false. No quotations are necessary when declaring a boolean.

{% assign foo = true %}
{% assign bar = false %}

 

Nil

Nil is an 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 for the truthfulness of a statement. The example below shows a situation where a fulfillment does not yet have a tracking number entered. The if statement would not render the included text within it.

{% if fulfillment.tracking_numbers %}
We have a tracking number!
{% endif %}

Any tags or outputs that return nil will not show anything on the screen.

Input

Tracking number: {{ fulfillment.tracking_numbers }}

Output

Tracking number: 

Arrays

Arrays hold a list of variables of all types.

Accessing all items in an array

To access items in an array, you can loop through each item in the array using a for tag or a tablerow tag.

Input

<!-- if product.tags = "sale", "summer", "spring", "wholesale" -->
{% for tag in product.tags %}
 {{ tag }}
{% endfor %}

Output

sale summer spring wholesale

Accessing a specific item in an array

You can use square brackets ( [ ] ) notation to access a specific item in an array. Array indexing starts at zero.

Input

<!-- if product.tags = "sale", "summer", "spring", "wholesale" -->
{{ product.tags[0] }} 
{{ product.tags[1] }} 
{{ product.tags[2] }} 
{{ product.tags[3] }} 

Output

sale
summer
spring
wholesale

Initializing an array

It is not possible to initialize an array in Liquid. For example, in Javascript you could do something like this:

<script>
var cars = ["Saab", "Volvo", "BMW"];
</script>

In Liquid, you must instead use the split filter to break a single string into an array of substrings. See here for examples.

EmptyDrop

An EmptyDrop object is returned whenever you try to access a non-existent object (for example, a collection, page or blog that was deleted or hidden) by handle. In the example below, page_1page_2 and page_3are all EmptyDrop objects.

{% assign variable = "hello" %}
{% assign page_1 = pages[variable] %}
{% assign page_2 = pages["i-do-not-exist-in-your-store"] %}
{% assign page_3 = pages.this-handle-does-not-belong-to-any-page %}

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.

Applications in themes

Using the empty? attribute, you can check to see if a page exists or not before accessing any of its other attributes.

{% unless pages.frontpage.empty? %}
 <!-- We have a page with handle 'frontpage' and it's not hidden.-->
 <h1>{{ pages.frontpage.title }}</h1>
 <div>{{ pages.frontpage.content }}</div>
{% endunless %}

It is important to see if a page exists or not first to avoid outputting empty HTML elements to the page, as follows:

<h1></h1>
<div></div>

You can perform the same verification with collections as well:

{% unless collections.frontpage.empty? %}
 {% for product in collections.frontpage.products %}
 {% include 'product-grid-item' %}
 {% else %}
 <p>We do have a 'frontpage' collection but it's empty.</p>
 {% endfor %}
{% endunless %}
Facebook Share Tweet

Was this article helpfu?

Yes No

Thank you for voting

×
Select company

You are related to multiple companies. Please select the company you wish to login as.