Yoolk Liquid

Home Basics Types

Types

Liquid objects can return one of six types: String, Number, Boolean, Nil, or Array. 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 listing does not yet have an email entered. The if statement would not render the included text within it.

{% if listing.email %}
We have an email address!
{% endif %}

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

Input

Tracking number: {{ listing.email.value }}

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:

Output

<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.