Data types
In Liquid, different kids of data have different types. Each type describes the nature of its data.

String

A string contains a series of characters, forming text.
1
{% assign my_name = "Matt" %}
Copied!

Integer, Float

Liquid supports both two number types: integers (whole numbers) and floats (numbers having decimal precision).
1
{% assign a_int = 99 %}
2
{% assign a_float = 99.99 %}
Copied!

Boolean

1
{% assign is_mechanic_awesome = true %}
2
{% assign is_it_warm_outside = false %}
Copied!

Nil

Borrowing from Ruby's concept of nil, Liquid's nil is an empty value that is returned when Liquid code has no results. It evaluates to false in conditionals statements, and outputs nothing when printing out text.
1
{% if order.email %}
2
There is an email address
3
{% endif %}
Copied!
Liquid
Output when not nil
Output when nil
1
email address: {{ order.email }}
Copied!
1
email address: [email protected]
Copied!
1
email address:
Copied!

Array

An array is a value that itself contains an ordered list of other values. Each value has an index, representing the order in which each value occurs in the list.

Creating an array

Liquid supports creating arrays of strings using the split filter. In Mechanic, arrays can be created using the array literal.

Iterating through arrays

1
{% for customer_tag in customers.tags %}
2
{{ customer_tag }}
3
{% endfor %}
Copied!

Accessing an array element

1
{{ customers.tags[0] }}
2
{{ customers.tags[1] }}
Copied!

Array filters

Mechanic includes a variety of array filters, useful for transforming arrays or retrieving specific values.

Object

An object is any value that has attributes (also known as properties). The name of an attribute is known as its key; the data stored for an attribute is known as its value. In Mechanic, some objects have additional intelligence of their own, like the shop object.

Iterating through objects

Objects may be traversed using for loops.
1
{% assign object = hash %}
2
{% assign object["foo"] = "bar" %}
3
{% assign object["baz"] = "qux" %}
4
​
5
{% for keyval in object %}
6
{% assign key = keyval[0] %}
7
{% assign value = keyval[1] %}
8
​
9
{{ key }}: {{ value }}
10
{% endfor %}
Copied!

Accessing an object property

All of the following examples return the same value.
1
{% assign foo = '{"foo":"bar"}' | parse_json %}
2
​
3
1. {{ foo.bar }}
4
2. {{ foo["bar"] }}
5
​
6
{% assign key = "bar" %}
7
3. {{ foo[key] }}
Copied!

Hash

In Mechanic, a hash is a simple type of object that has no additional intelligence at all; it only contains keys and values. It can be constructed by the developer using code.

Creating a hash

In Mechanic, hashes can be created using the hash literal, or by using any of the "parse" filters.
This only applies to Mechanic. Liquid objects and hashes cannot be created in Shopify.

Iterating through hashes

Hashes may be traversed using for loops, like other objects. For convenience, Mechanic also supports extract an array of hash keys using the keys filter, or object values using the values filter. The resulting arrays may also be used with a for loop, like any array.
1
{% assign object = hash %}
2
{% assign object["foo"] = "bar" %}
3
{% assign object["baz"] = "qux" %}
4
​
5
{% for keyval in object %}
6
{% assign key = keyval[0] %}
7
{% assign value = keyval[1] %}
8
​
9
{{ key }}: {{ value }}
10
{% endfor %}
11
​
12
{% assign keys = object | keys %}
13
{% for key in keys %}
14
{{ key }}: {{ object[key] }}
15
{% endfor %}
16
​
17
{% assign values = object | values %}
18
{% for value in values %}
19
{{ value }}
20
{% endfor %}
Copied!
Last modified 7mo ago