Shopify Liquid Operators & Controls
Shopify Liquid operators & controls allow developers to create conditional logic, loops, and comparisons inside Shopify themes. This guide covers all essential operators with examples!
Shopify Liquid Operators
Liquid includes many logical and comparison operators. You can use operators to create logic with control flow tags.
| Operator | Description | Example |
|---|---|---|
| Equal to |
|
| Not equal to |
|
| Greater than |
|
| Less than |
|
| Greater than or equal |
|
| Less than or equal |
|
| One condition must be true |
|
| Both conditions must be true |
|
| Checks if a string or array contains a value |
|
Control Flow Statements
Control flow tags create conditions that decide whether blocks of Liquid code get executed.
if Statement
Executes a block of code only if a certain condition is true.
Code
{% if product.title == "Awesome Shoes" %}
These shoes are awesome!
{% endif %} Output
These shoes are awesome!
if else Statement
if a certain condition is true then do something otherwise do other things.
Code
{% if product.title == "Awesome Shoes" %}
These shoes are awesome!
{% else %}
Regular shoes
{% endif %}
Output
Regular shoes
elsif / else Statement
Adds more conditions within an if or unless block.
Code
<!-- If customer.name = "anonymous" -->
{% if customer.name == "kevin" %}
Hey Kevin!
{% elsif customer.name == "anonymous" %}
Hey Anonymous!
{% else %}
Hi Stranger!
{% endif %}
Output
Hey Anonymous!
unless Statement
The opposite of if – executes a block of code only if a certain condition is not met.
Code
{% unless product.title == "Awesome Shoes" %}
These shoes are not awesome.
{% endunless %}
Output
These shoes are not awesome.
This would be the equivalent of doing the following:
{% if product.title != "Awesome Shoes" %}
These shoes are not awesome.
{% endif %}
case/when Statement
-
The
casestatement is used for conditional branching, similar toswitchin other programming languages. -
whenkeyword is used to match the condition. -
A
whentag can accept multiple values. -
If a
whencondition matches the variable, its corresponding block of code is executed. -
The
else(orelsecase) is optional and runs if nowhenconditions match.
Code
{% assign handle = "cake" %}
{% case handle %}
{% when "cake" %}
This is a cake
{% when "cookie", "biscuit" %}
This is a cookie
{% else %}
This is not a cake nor a cookie
{% endcase %}
Output
This is a cake