Conditional Tags


Conditional tags specify conditions that decide if blocks of Liquid code should run or not.


If

Executes a block of code only when a specific condition is true.

{%- if product.title == 'Awesome Shoes' -%}
These shoes are awesome!
{%- endif -%}

elsif

You can utilize the elsif tag to evaluate several conditions.

{%- if product.type == 'Love' -%}
This is a love potion!
{%- elsif product.type == 'Health' -%}
This is a health potion!
{%- endif -%}

elsif

Lets you set a default action when no other conditions apply.

You can use the else tag with these tags:

  • case
  • if
  • unless
{%- else -%}
expression

The expression to show if no other conditions apply.

{%- if product.available -%}
This product is available!
{%- else -%}
This product is sold out!
{%- endif -%}

unless

Displays an expression unless a certain condition is true.

{%- unless condition -%}
expression
{%- endunless -%}

Note: Just like the if tag, you can use elsif to add more conditions to an unless tag.


case

Shows a specific output based on the value of a given variable.

{%- case variable -%}
{%- when first_value -%}
first_expression
{%- when second_value -%}
second_expression
{%- else -%}
third_expression
{%- endcase -%}

variable

The name of the variable you intend to use as the basis for your case statement.

first_value

A specific value to check for.

second_value

A specific value to check for.

first_expression

An expression displayed when the variable's value equals first_value.

second_expression

An expression displayed if the variable's value equals second_value.

third_expression

An expression shown when the variable doesn't match any value.

{%- case product.type -%}
{%- when 'Health' -%}
This is a health potion.
{%- when 'Love' -%}
This is a love potion.
{%- else -%}
This is a potion.
{%- endcase -%}

Multiple values

{%- case variable -%}
{%- when first_value or second_value or third_value -%}
first_expression
{%- when fourth_value, fifth_value, sixth_value -%}
second_expression
{%- else -%}
third_expression
{%- endcase -%}

A when tag can accept multiple values. When multiple values are provided, the expression is returned when the variable matches any of the values inside of the tag. Provide the values as a comma-separated list, or separate them using an or operator.

{%- case product.type -%}
{%- when 'Love' or 'Luck' -%}
This is a love or luck potion.
{%- when 'Strength','Health' -%}
This is a strength or health potion.
{%- else -%}
This is a potion.
{%- endcase -%}