Data types
In Liquid, different kinds of data have different types. Each type describes the nature of its data.
Some of the documentation below is specific to Mechanic Liquid, and may not apply to Shopify Liquid, or to other Liquid implementations.
A string contains a series of characters, forming text.
{% assign my_name = "Matt" %}
Liquid supports both two number types: integers (whole numbers) and floats (numbers having decimal precision).
{% assign a_int = 99 %}
{% assign a_float = 99.99 %}
{% assign is_mechanic_awesome = true %}
{% assign is_it_warm_outside = false %}
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.If a variable reference comes up missing, Liquid will silently use
nil
instead without raising an error.In Liquid,
null
is not a keyword literal. But, because null
is also typically not used as a variable name in Mechanic Liquid code, and because Liquid uses nil
when a variable is not found, it works out: {% assign foo = null %}
results in assigning foo
a value of nil
. (Unless, of course, null
was previously assigned to something else, e.g. {% assign null = "something else" %}
.){% if order.email %}
There is an email address
{% endif %}
Liquid
Output when not nil
Output when nil
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.
Liquid supports creating arrays of strings using the split filter. In Mechanic, arrays can be created using the array literal.
The array literal is unique to Mechanic Liquid. Arrays cannot be created in this way using Shopify Liquid.
{% assign an_array_of_strings = "one,two,three" | split: "," %}
{% assign also_an_array_of_strings = "1,2,3" | split: "," %}
{% assign an_empty_array = array %}
{% for customer_tag in customers.tags %}
{{ customer_tag }}
{% endfor %}
{{ customers.tags[0] }}
{{ customers.tags[1] }}
Mechanic includes a variety of array filters, useful for transforming arrays or retrieving specific values.
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.
{% assign object = hash %}
{% assign object["foo"] = "bar" %}
{% assign object["baz"] = "qux" %}
{% for keyval in object %}
{% assign key = keyval[0] %}
{% assign value = keyval[1] %}
{{ key }}: {{ value }}
{% endfor %}
All of the following examples return the same value.
{% assign foo = '{"foo":"bar"}' | parse_json %}
1. {{ foo.bar }}
2. {{ foo["bar"] }}
{% assign key = "bar" %}
3. {{ foo[key] }}
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.
This only applies to Mechanic Liquid. Hashes cannot be created in Shopify Liquid.
{% assign a_hash_object = hash %}
{% assign a_hash_object["key"] = "value" %}