Operators are the words and symbols used to write a condition, in if, unless and elsif tags. (Yes, elsif is one word, with no ending ‘e’ on ‘else’!)
1
{% if price > 10 %}Premium{% endif %}
Operators can’t be used inside {{ ... }} output, and they can’t do maths. For that, use the maths filters.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
== |
is equal to | {% if status == "live" %} |
!= |
is not equal to | {% if status != "draft" %} |
<> |
is not equal to (same as !=) |
{% if status <> "draft" %} |
> |
is greater than | {% if copies > 100 %} |
< |
is less than | {% if copies < 100 %} |
>= |
is greater than or equal to | {% if copies >= 100 %} |
<= |
is less than or equal to | {% if copies <= 100 %} |
Strings are case-sensitive. "Live" == "live" is false. If case shouldn’t matter, convert both sides first with the downcase filter (see the filters page).
Compare like with like. A number and a string are never equal, even when they look the same: 5 == "5" is false. However, whole numbers and decimals can be compareed properly. 5 == 5.0 is true.
Troubleshooting
Comparing different types of data
<, >, <= and >= need two numbers, or two strings. Mixing them produces an error message:
1
{% if "5" > 3 %}Bigger{% endif %}
1
Liquid error: comparison of String with 3 failed
Ordering strings
Strings are ordered alphabetically, with capital letters sorting before lowercase ones. So "apple" < "banana" is true, but "Zebra" < "apple" is also true.
Comparing with nil
{% if work.subtitle == nil %} is true when the subtitle has no value. To test for nil or empty values, use blank.
contains
contains checks whether something is inside a string or an array.
1
{% if title contains "Guide" %}This is a guide.{% endif %}
1
{% if formats contains "audio" %}Available in audio.{% endif %}
Troubleshooting
- Contains is case-sensitive.
"The Hobbit" contains "hobbit"is false. - In an array, the type matters. An array of numbers
[1, 2, 3]does not contain the string"1". In a string, though, numbers are converted for you:"Edition 1" contains 1is true. - It matches whole items, not parts of them. If an array holds objects,
containsonly finds an item that is entirely equal to the value you give. It can’t look at a property inside each object. To find objects by a property, use thewherefilter (see the filters page.)
Does not contain
Use the unless tag instead:
1
{% unless work.themas contains "FM" %}Sorry, not in this month's Fantasy promotion.{% endunless %}
Combining conditions: and and or
Use and when both conditions must be true, and or when either one is enough (note, this is simplified to explain the concept and is not valid for Consonance!):
1
2
{% if status == "live" and copies > 0 %}On sale{% endif %}
{% if format == "hardback" or format == "paperback" %}Print edition{% endif %}
Order of evaluation
When you use more than one and/or, Liquid works from right to left. Please note that this is different from ordinary maths and logic.
1
{% if false and true or true %}Yes{% else %}No{% endif %}
Liquid reads this as false and (true or true), which is false, so it prints No.
Reversing conditions
Liquid has no not operator. To reverse a condition:
- use
!=foris not equal to
- use
unlessforif this is not true
- compare with
blankforhas content
(!= blank)
1
{% unless status == "draft" %}Published{% endunless %}
Quick reference
| Question | Solution |
|---|---|
| Are these equal? | a == b |
| Are these different? | a != b |
| Is this bigger or smaller? | a > b, a < b, a >= b, a <= b |
| Is this text inside that text? | text contains "piece" |
| Is this value in that array? | array contains "value" |
| Are both true? | a and b |
| Is either true? | a or b |
| Does it have any content? | a != blank |
| Is it empty or has no value? | a == blank |
| Is it not in the array? | {% unless array contains "value" %} |