Operators
An operator is a statement that evaluates some code, and makes a decision about what to do next. Operators can be used for comparing two values, inspecting a single value, or evaluating whether or not some code returns true.
For further discussion on how Liquid treats values, see their documentation: Truthiness and falsiness in Liquid.

Comparison operators

If b = 6, the table below illustrates the comparison operators and the way they interact with b:
Operator
Description
Example
Result
==
equal to
b == 6
true
!=
not equal
b != 6
false
>
greater than
b > 5
true
<
less than
b < 6
false
>=
greater than or equal to
b >= 6
true
<=
less than or equal to
b <= 6
true

Examples

1
{% assign order_qualifies = false %}
2
​
3
{% if order.cancelled_at == blank and cancel_risk %}
4
{% assign order_qualifies = true %}
5
{% endif %}
Copied!
1
{% if has_product != true %}
2
{% log "Order has no products; skipping" %}
3
{% assign send_email = false %}
4
{% endif %}
Copied!

Testing for blank, empty

Liquid supports equality/inequality testing a value against blank or empty. In this way, Liquid allows the author to ask, "is this value blank?", or "is this value empty?"

Blank

Quoting from the Rails API:
An object is blank if it's false, empty, or a whitespace string. For example, nil, '', ' ', [], {}, and false are all blank.
1
{% assign empty_string = "" %}
2
{% assign whitespace_string = " " %}
3
{% assign non_blank_string = " !! " %}
4
{% assign non_empty_array = "1,2,3" | split: "," %}
5
{% assign empty_hash = hash %}
6
​
7
{% comment %}
8
The following `if` statements all pass.
9
{% endcomment %}
10
{% if empty_string == blank %}
11
{% if whitespace_string == blank %}
12
{% if non_blank_string != blank %}
13
{% if non_empty_array != blank %}
14
{% if empty_hash == blank %}
Copied!

Empty

An array, hash, or string can be tested for emptiness. An array is empty if it has no elements; a hash is empty if it has no key-value pairs; a string is empty if its length is zero.
A string that only contains whitespace is not empty, but it is blank!
1
{% assign empty_string = "" %}
2
{% assign whitespace_string = " " %}
3
{% assign non_blank_string = " !! " %}
4
{% assign non_empty_array = "1,2,3" | split: "," %}
5
{% assign empty_hash = hash %}
6
​
7
{% comment %}
8
The following `if` statements all pass.
9
{% endcomment %}
10
{% if empty_string == empty %}
11
{% if whitespace_string != empty %}
12
{% if non_blank_string != empty %}
13
{% if non_empty_array != empty %}
14
{% if empty_hash == empty %}
Copied!

Contains operator

The contains operator is used to check for the existence of a substring in a string, or for a specific element in an array.

Substring containment

1
{% if email_domain contains "@" %}
2
{% error "Do not include '@' symbols in email domains. Thanks!" %}
3
{% endif %}
Copied!

Array element containment

1
{% assign product_tags = "foo, bar, baz" | split: ", " %}
2
{% assign product_is_tagged_bar = false %}
3
​
4
{% if product_tags contains "bar" %}
5
{% assign product_is_tagged_bar = true %}
6
{% endif %}
Copied!

Logical operators

Liquid has two logical operators: and and or.
If b = 3 and c = 6, the table below illustrates the logical operators in Liquid, and the way they interact with b and b:
Operator
Example
Result
and
b < 2 and c < 6
false
or
b < 2 or c < 6
true

Check if both the shipping address and billing are blank

1
{% if order.shipping_address != blank and order.billing_address != blank %}
2
{% comment %} do something {% endcomment %}
3
{% endif %}
Copied!

Check if tag equals blank or a customer's tags contains a certain tag

1
{% if tag == blank or customer.tags contains tag %}
2
{% comment %} do something {% endcomment %}
3
{% endif %}
Copied!
Last modified 5mo ago