Operators

Operators in Liquid are essential for comparing and combining variables in theme templates. They enable theme developers to create logic and conditional statements. This document provides an overview of the available operators in Liquid and their usage.

1. Arithmetic Operators

Arithmetic operators perform mathematical operations between two values or variables.

1.1. Addition (+)

Adds two values.

Example:

{{ product.price + additional_price }}

1.2. Subtraction (-)

Subtracts one value from another.

Example:

{{ product.price - discount }}

1.3. Multiplication (*)

Multiplies two values.

Example:

{{ product.price * quantity }}

1.4. Division (/)

Divides one value by another.

Example:

{{ product.price / installments }}

1.5. Modulo (%)

Returns the remainder of a division operation.

Example:

{{ product.reviews_count % 10 }}

2. Comparison Operators

Comparison operators are used to compare two values or variables.

2.1. Equal (==)

Returns true if the values on both sides of the operator are equal.

Example:

{% if product.price == 0 %}
  Free
{% endif %}

2.2. Not Equal (!=)

Returns true if the values on both sides of the operator are not equal.

Example:

{% if product.price != 0 %}
  Paid
{% endif %}

2.3. Greater Than (>)

Returns true if the value on the left is greater than the value on the right.

Example:

{% if product.inventory_quantity > 0 %}
  In stock
{% endif %}

2.4. Less Than (<)

Returns true if the value on the left is less than the value on the right.

Example:

{% if product.inventory_quantity < 10 %}
  Low stock
{% endif %}

2.5. Greater Than or Equal To (>=)

Returns true if the value on the left is greater than or equal to the value on the right.

Example:

{% if product.rating >= 4 %}
  Highly rated
{% endif %}

2.6. Less Than or Equal To (<=)

Returns true if the value on the left is less than or equal to the value on the right.

Example:

{% if product.rating <= 2 %}
  Poorly rated
{% endif %}

3. Logical Operators

Logical operators are used to combine multiple conditions.

3.1. AND (and)

Returns true if both conditions are true.

Example:

{% if product.available and product.featured %}
  Featured and available
{% endif %}

3.2. OR (or)

Returns true if at least one of the conditions is true.

Example:

{% if product.on_sale or product.clearance %}
  On sale or clearance
{% endif %}

Summary

Understanding and using operators in Liquid is crucial for creating dynamic, responsive, and flexible theme templates. By using arithmetic, comparison, and logical operators, you can build complex logic and conditional statements to tailor your theme's appearance and behavior to specific scenarios.

Last updated