Skip to main content
Control flow and condition evaluation:

Liquid logical and comparison operators

Summary

This article explains how logical and comparison operators work in the Liquid templating language. You will learn how to use them to evaluate expressions and control content flow.

Introduction #

Liquid’s logical and comparison operators allow you to control the flow of your templates by evaluating expressions and conditions.

This article explains how Liquid logical and comparison operators work, and how you can apply them in your templates to render dynamic content.

Liquid comparison operators #

Comparison operators in Liquid are used to compare values. The result is always a boolean value.

Available comparison and logical operators:

OperatorMeaningDescriptionApplies to
==Equalstrue if the two values are equalStrings, numbers
!=Not equalstrue if the two values are not equalStrings, numbers
>Greater thantrue if the left value is greater than the rightNumbers
<Less thantrue if the left value is less than the rightNumbers
>=Greater than or equal totrue if the left value is greater or equalNumbers
<=Less than or equal totrue if the left value is less or equalNumbers
containsContainstrue if a string or array includes a valueStrings, arrays
andLogical ANDtrue if both expressions are trueBoolean expressions
orLogical ORtrue if either expression is trueBoolean expressions

You can use these operators in conditional statements to control output, e.g.:

== equals #

This checks if the user’s name is exactly “Alice”:

{% if user.name == "Alice" %}
  Hello, Alice.
{% endif %}

!= not equals #

This displays the content for any user who is not a guest:

{% if user.role != "guest" %}
  You have access.
{% endif %}

> greater than #

This renders if the order quantity is more than 5:

{% if order.quantity > 5 %}
  Bulk discount applies.
{% endif %}

< less than #

This triggers when the number of items in the cart is less than 1:

{% if cart.items < 1 %}
  Your cart is empty.
{% endif %}

>= greater than or equal to #

This condition is true if the user is 18 or older:

{% if user.age >= 18 %}
  You are allowed to vote.
{% endif %}

<= less than or equal to #

This is true when the stock is 10 or less:

{% if product.stock <= 10 %}
  Only a few items left.
{% endif %}

contains operator #

This checks whether the tags array of a product includes the value "sale":

{% if product.tags contains "sale" %}
  This product is on sale.
{% endif %}

and logical AND #

This displays content only if both conditions are true.

{% if user.verified and user.subscribed %}
  Thank you for being a verified subscriber.
{% endif %}

or logical OR #

This will render content if the user is either an admin or staff member.

{% if user.admin or user.staff %}
  You have staff access.
{% endif %}

Liquid logical operators #

Logical operators in Liquid are used to combine or modify the outcome of multiple conditional expressions.

They return boolean values (true or false).

The two logical operators are:

  • and: Returns true if both expressions are true.
  • or: Returns true if at least one of the expressions is true.

Order of operations #

Liquid does not support parentheses for grouping expressions. Using parentheses will result in a syntax error and your tag will not render.

When you use multiple and or or operators within a single tag, they are evaluated from right to left.

Example:

{% if true or false and false %}
  This evaluates to true, because `false and false` is false, and `true or false` is true.
{% endif %}

In this case, false and false is evaluated first, followed by true or false.

Another example:

{% if true and false and false or true %}
  This evaluates to false, because the expression is interpreted as:

  true and (false and (false or true))
  true and (false and true)
  true and false
  false
{% endif %}

Since Liquid evaluates from right to left, what looks like grouped logic in other languages will behave differently in Liquid. Always structure your conditions accordingly.

Using logical and comparison operators together #

You can combine logical and comparison operators to build complex conditions. Use parentheses to clarify which parts of the condition should be evaluated first.

For examples, this checks if the product is available and either has enough stock or can be backordered:

{% if product.available and (product.stock > 10 or product.backorderable) %}
  Product can be purchased.
{% endif %}

FAQ's #

Most common questions and brief, easy-to-understand answers on the topic:

What is the purpose of logical operators in Liquid?

Logical operators in Liquid allow you to combine or evaluate multiple conditions using and and or.

Can I use == in Liquid to compare numbers and strings?

Yes, the == operator can be used to compare both numbers and strings in Liquid.

What is the difference between == and != in Liquid?

== checks for equality, while != checks for inequality between two values.

Does Liquid support greater than or equal comparisons?

Yes, Liquid supports >= (greater than or equal) and <= (less than or equal) operators.

Can I nest logical and comparison operators in Liquid?

Yes, you can nest expressions using parentheses for clarity and control the order of operations in Liquid.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

Liquid logical and comparison operators by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/liquid-operators">Liquid logical and comparison operators</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the Ditig legal page.

All Topics

Random Quote

“When providing information, make sure your site has something unique to offer.”

Sally Hambridge Chair of the IETF Network Working GroupIETF RFC 1855 - Netiquette Guidelines, - IT quotes