Conditional
Conditional tags define conditions that determine whether blocks of Liquid code get executed.
if
Renders an expression if a specific condition is true
.
{% if condition %}
expression
{% endif %}
condition
: The condition to evaluate.expression
: The expression to render if the condition is met.
{% if product.compare_at_price > product.price %}
This product is on discount!
{% endif %}
{
"product": {
"compare_at_price": "18.88",
"price": "12.88"
}
}
This product is on discount!
elsif
You can use the elsif
tag to check for multiple conditions.
{% if product.type == 'Seafood' %}
This is a seafood type!
{% elsif product.type == 'Fruit' %}
This is a fruit type!
{% endif %}
{
"product": {
"type": "Fruit"
}
}
This is a fruit type!
else
Allows you to specify a default expression to execute when no other condition is met.
You can use the else
tag with the following tags:
{% else %}
expression
expression
: The expression to render if no other condition is met.
{% if product.available %}
This product is available!
{% else %}
This product is sold out!
{% endif %}
{
"product": {
"available": false
}
}
This product is sold out!
unless
Renders an expression unless a specific condition is true
.
Tip
Similar to the if tag, you can use
elsif
to add more conditions to anunless
tag.
{% unless condition %}
expression
{% endunless %}
condition
: The condition to evaluate.expression
: The expression to render unless the condition is met.
{% unless product.has_only_default_variant %}
Variant selection functionality
{% endunless %}
{
"product": {
"has_only_default_variant": false
}
}
Variant selection functionality
case
Renders a specific expression depending on the value of a specific variable.
{% case variable %}
{% when first_value %}
first_expression
{% when second_value %}
second_expression
{% else %}
third_expression
{% endcase %}
variable
: The name of the variable you want to base your case statement on.first_value
: A specific value to check for.second_value
: A specific value to check for.first_expression
: An expression to be rendered when the variable's value matchesfirst_value
.second_expression
: An expression to be rendered when the variable's value matchessecond_value
.third_expression
: An expression to be rendered when the variable's value has not match.
{% case product.type %}
{% when 'Seafood' %}
This is a seafood type.
{% when 'Fruit' %}
This is a fruit type.
{% else %}
This is a food.
{% endcase %}
{
"product": {
"type": "Fruit"
}
}
This is a fruit type.
Updated over 1 year ago