09-Jul-2025 | v7.3.5 | All new article
Liquid Logic
Control the output of data and the rendering of your HTML with liquid conditional logic statements.
if
The if
statement is a fundamental control flow structure that allows for the conditional execution of code.
It evaluates a specified condition, using conditional operators, and if that condition is determined to be "truthy", a block of code associated with the if
statement is executed. If the condition is "falsy", the code block within the if statement is either skipped or moves on to else
executions.
{% if request.is_logged == true %}
You are logged in!
{% endif %}
You are logged in!
elsif / else
Extends the if
logic to execute alternative conditions (elsif
), or fallback code (else
), if the preceeding condition/s are not met.
{% if request.currentmember.country == "Australia" %}
Great to see someone from the land down under!
{% elsif request.currentmember.country == "United States" %}
Keep living that American Dream!
{% else %}
Welcome {{ request.currentmember.firstname }}!
{% endif %}
Welcome Alex!
In the above example, the currently logged in member's country did not match the string "Australia" or "United States", so the else
fallback executed.
Be aware that the "else if" control keyword omits an 'e' to become a single word elsif
, unlike other programming languages which use the full term else if
or elseif
.
unless
Generally, unless
is the opposite of if
and can be used as a shorter conditional block when the code execution simply requires the condition not being met.
This is often handy where code is expected to execute in majority of cases with few exceptions.
{% assign myInterests = "Singing,Dancing,Stargazing" | split: ',' %}
{% for topic in myInterests %}
{{ topic }}
{% unless forloop.last %}
<hr>
{% endunless %}
{% endfor %}
Singing
Dancing
Stargazing
The unless
control here simply omits the horizontal rule on the very last loop, allowing it in all other loops.
This can makes your code cleaner, as opposed to writing a redundant if
condition, such as:
{% for topic in myInterests %}
{{ topic }}
{% if forloop.last %}
<!-- do nothing -->
{% else %}
<hr>
{% endif %}
{% endfor %}
Singing
Dancing
Stargazing
Unlike the if
control, unless
does not support else
or elsif
within its control structure.
case
Creates a switch statement to execute the code block when a single variable has a specified value. case
initialises the switch statement, and when
statements define the various conditions.
A when tag can accept multiple values. When multiple values are provided, the expression is returned when the variable matches any of the values listed. The values should be separated using the or
operator.
An optional else
statement at the end of the case
provides code to execute if none of the conditions are met.
{% assign foodType = "biscuit" %}
{% case foodType %}
{% when "cake" %}
Yum, cake!
{% when "cookie" or "biscuit" %}
I can't decide, cookie or biscuit?
{% else %}
Hmm, this isn't a food worth eating :(
{% endcase %}
I can't decide, cookie or biscuit?
Operators
The following operators represent various condition types you can build into your Liquid logic blocks.
==
!=
>
<
>=
<=
contains
or
and
Examples
The below condition resolves to true
. Even though the age is not less than 16, the operator is checking for an equal match as well (less than or equal to).
{% assign myAge = 16 %}
{% if myAge <= 16 %}
I am considered an adolescent.
{% endif %}
I am considered an adolescent.
Multiple comparisons can be combined in one Liquid tag by use of the logical operators and
and or
:
{% assign myAge = 16 %}
{% if myAge > 18 and myAge < 60 %}
I am considered an adult, but probably not old enough to retire.
{% else %}
I am either not an adult yet, or am possibly retired.
{% endif %}
I am either not an adult yet, or am possibly retired.
Multiple comparisons when using the contains
operator checking an array.
{% assign myInterests = "Singing,Dancing,Stargazing" | split: ',' %}
{% assign myAge = 16 %}
{% if myAge > 18 and myInterests contains "Dancing" %}
I'm over 18 and interest in dancing.
{% else %}
I'm either not over 18 or I do not like dancing.
{% endif %}
I'm either not over 18 or I do not like dancing.
Order of Operations
In tags with more than one and
or or
operator, all and
operators are checked first followed by the or
operators. You cannot use parentheses ()
to prioritise the order of operations (parentheses are invalid characters in Liquid and will prevent your tags from working).
Therefore, complex logic expressed in a single conditional statement can lead to unexpected results. The best and safest practice in Liquid is to avoid mixing and
and or
in the same conditional block. Instead, break the logic into separate, clear steps using if
, elsif
, or nested if
statements.
In the below example, we want to check if the logged in member qualifies for a discount. They need to either be a VIP member OR live in Australia AND their cart total must be more than or equal to $100 to qualify.
Rather than try to construct a single conditional statement for this combining both OR and AND conditions, let's break it into two steps so we can dictate our own logical priority.
{% assign cartTotal = cartSummaryData.grandTotalPrice %} <!-- "190.9500" -->
{% assign status = request.currentmember.status %} <!-- "VIP" -->
{% assign country = request.currentmember.country %} <!-- "France" -->
{% assign vipOrAU = status == 'VIP' or country == 'Australia' %} <!-- true -->
{% if cartTotal >= 100 and vipOrAU %}
You qualify for the discount!
{% endif %}
You qualify for the discount!
Truthy / Falsy
When a non-boolean data type is used in a boolean context (such as a conditional tag), Liquid decides whether to evaluate it as true
or false
. Data types that return true by default are called truthy. Data types that return false by default are called falsy.
All values in Liquid are truthy except null
and false
, which are falsy.
In the example below, the text “Alex” is not a boolean, but it is truthy in a conditional:
{% assign name = "Alex" %}
{% if name %}
My name is {{ name }}.
{% endif %}
My name is Alex.
Strings, even when empty, are truthy.
The example below will execute, even though the variable is empty, as it still exists (is defined):
{% assign name = "" %}
{% if name %}
My name is {{ name }}.
{% endif %}
My name is .