Operators
Comparison operators, logical operators, contains, and type coercion rules.
Operators let you compare values and combine conditions. Use them inside if, elsif, unless, and case tags.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
== | Equal | {% if product.type == 'physical' %} |
!= | Not equal | {% if product.type != 'digital' %} |
> | Greater than | {% if product.price > 5000 %} |
< | Less than | {% if product.price < 1000 %} |
>= | Greater than or equal | {% if cart.item_count >= 3 %} |
<= | Less than or equal | {% if product.price <= 2999 %} |
Examples
Show a free shipping badge for orders over a threshold:
{% if cart.total_price >= 5000 %}
<span class="badge">Free Shipping</span>
{% endif %}Compare strings:
{% if product.vendor == 'Auricle' %}
<span class="badge">Our Brand</span>
{% endif %}Logical operators
Combine multiple conditions with and or or:
and
Both conditions must be true:
{% if product.available and product.price > 0 %}
<button>Add to Cart</button>
{% endif %}or
At least one condition must be true:
{% if product.type == 'digital' or product.type == 'service' %}
<p>No shipping required</p>
{% endif %}Important: no mixing
You cannot mix and and or in one expression. This will not work:
<!-- Don't do this -->
{% if a and b or c %}Use nested if tags instead:
{% if product.available %}
{% if product.price > 0 or product.type == 'service' %}
<button>Buy Now</button>
{% endif %}
{% endif %}contains
Check if a string includes a substring:
{% if product.title contains 'Edition' %}
<span class="badge">Special Edition</span>
{% endif %}Check if an array includes a value:
{% if product.tags contains 'sale' %}
<span class="badge">On Sale</span>
{% endif %}contains is case-sensitive. 'Sale' and 'sale' are different values.
Truthy and falsy
Only two values are falsy in Quill:
nil(no value, null)false
Everything else is truthy, including:
0(zero)""(empty string)[](empty array)
Keep this in mind. To check for an empty string, use == rather than a plain if:
<!-- This will be TRUE even for an empty string -->
{% if product.vendor %}
<p>{{ product.vendor }}</p>
{% endif %}
<!-- Use this to check for an empty string -->
{% if product.vendor != blank %}
<p>{{ product.vendor }}</p>
{% endif %}The special value blank matches both nil and empty strings. Use it when you want to treat both the same way.
blank and empty
| Value | Matches |
|---|---|
blank | nil, false, and empty strings ("") |
empty | Empty arrays and empty strings |
{% if collection.products == empty %}
<p>No products in this collection yet.</p>
{% endif %}{% unless product.description == blank %}
<div class="description">
{{ product.description }}
</div>
{% endunless %}Operator precedence
Conditions are read left to right. There is no order of operations. This is why and and or cannot be mixed.
For complex conditions, break them into steps with assign:
{% assign is_physical = false %}
{% if product.type == 'physical' or product.type == 'bundle' %}
{% assign is_physical = true %}
{% endif %}
{% if is_physical and product.available %}
<p>Ships in 2-3 days</p>
{% endif %}This is easier to read and avoids ambiguity.