Insites Docs Developers liquid Liquid TagsLiquid Tags: Flow Control

Liquid Tags: Flow Control

Last updated on June 18, 2024

We discuss the following topics in this article:

  • If/Else
  • Operators
  • Boolean Operators
  • Types and Truthiness
  • Case Statement

If/Else

if / else statements should be similar to other programming languages. Liquid implements them with the following tags:

  • : Encloses a section of a template that will only be run if the condition is true.
  • : Can optionally be used within an ... block. Specifies another condition; if the initial 'if' fails, Liquid tries the 'elsif' and runs the following section of the template if it succeeds. You can use any number of elsifs in an if block.
  • : Can optionally be used within an ... block, after any 'elsif' tags. If all preceding conditions fail, Liquid will run the section of the template following the 'else' tag.
  • : The reverse of an 'if' statement. Don't use 'elsif' or 'else' with an unless statement.

The condition of an if, elsif, or unless tag should be either a normal Liquid expression or a comparison using Liquid expressions. Note that the comparison operators are implemented by the 'if'-like tags; they don't work anywhere else in Liquid.

Operators

The available comparison operators are:

  • , , , and : equality and inequality (the latter two are synonyms)
  • , , , : less/greater-than
  • There are also special values and (unquoted) that you can compare arrays to; the comparison is if the array has no members.
  • : similar to Ruby's method, implemented on strings, arrays, and hashes. If the left argument is a string and the right isn't, it stringifies the right.
Important

Always be mindful of the Liquid syntax. For example, will work while (without the spaces) will not.

Boolean Operators

The available Boolean operators are:

You can use the and operators to include more than one condition in a control flow tag. and can be chained together to create complex conditionals.

If you use multiple or operators, note that operators will be evaluated first, then operators. You cannot use parentheses to simulate an order of operations and control the order of operator evaluation. Parentheses are invalid characters within Liquid tags and prevent your tags from working.

Important

Commonly used in other languages, and DO NOT work in Liquid.

and

The operator lets you add additional conditions to a tag. A condition with an will only be true if both the condition's left and right sides are true.

Input

Output

or

The operator lets you add additional conditions to a tag. A condition with an will be true if either the condition's left or right side is true.

Output

Note

There is NO operator. Also, note that you CAN NOT use parentheses to control the order of operations, and the precedence of the operators appears to be unspecified. So when in doubt, use nested "if" statements.

Types and Truthiness

Liquid expressions are tested for 'truthiness' in a Ruby-like way:

Valuetruthyfalsy
true
false
nil
string
empty string
0
integer
float
array
empty array
page
EmptyDrop

Case Statement

If you need more conditions, you can use the statement:

Example:

Note

This topic is a compilation of knowledge found at Shopify Themes, Liquid Documentation, Liquid Gem Documentation, and Liquid for Designers.

Have a suggestion for this page?

Didn't quite find what you are looking for or have feedback on how we can make the content better then we would love to hear from you. Please provide us feedback and we will get back to you shortly.