Tags

Prev Next

Tags are the structural and logical building blocks of a Liquid template. Denoted by {% %} delimiters, they govern control flow, iteration, and variable assignment, but unlike output blocks, they render no content to the page themselves.

if / elsif / else / endif

{% if condition %}
  ...
{% elsif other_condition %}
  ...
{% else %}
  ...
{% endif %}

Every if must be closed with {% endif %}.

{% if c_loyalty_tier == "gold" %}
  Gold member offer: free shipping on all orders.
{% elsif c_loyalty_tier == "silver" %}
  Silver member offer: free shipping over 500 TL.
{% else %}
  Standard shipping rates apply.
{% endif %}

unless

The opposite of if. It runs when the condition is false.

{% unless c_loyalty_tier == blank %}
  Welcome back, {{ c_loyalty_tier }} member!
{% endunless %}

This is equivalent to {% if c_loyalty_tier != blank %}. You cannot use elsif with unless.

case / when

You can match a single value against multiple options.

{% case c_loyalty_tier %}
  {% when "bronze" %}
    Welcome to our loyalty program!
  {% when "silver" %}
    Welcome, Silver member!
  {% when "gold" %}
    Welcome, Gold member — your exclusive benefits are active.
  {% else %}
    Join our loyalty program to start earning rewards.
{% endcase %}

You can match multiple values in one when using commas.

{% case language %}
  {% when "tr", "az" %}
    Merhaba!
  {% when "en", "au", "nz" %}
    Hello!
  {% else %}
    Hi!
{% endcase %}

The or keyword is not supported inside when. You can use commas instead.

for

You can loop over a list or a number range.

{% for item in items %}
  {{ item }}
{% endfor %}

Number range

{% for i in (1..5) %}
  {{ i }}
{% endfor %}
→  1 2 3 4 5

limit, offset, reversed

{% for item in items limit: 3 %}...{% endfor %}
{% for item in items offset: 2 %}...{% endfor %}
{% for item in items limit: 3 offset: 2 %}...{% endfor %}
{% for item in items reversed %}...{% endfor %}

break and continue

{% for item in items %}
  {% if item == "skip_me" %}{% continue %}{% endif %}
  {% if item == "stop_here" %}{% break %}{% endif %}
  {{ item }}
{% endfor %}

forloop variables

Variable

Description

forloop.index

Current position, starting from 1

forloop.index0

Current position, starting from 0

forloop.first

true on the first iteration

forloop.last

true on the last iteration

forloop.length

Total number of iterations

{% for item in items %}
  {% if forloop.first %}<ul>{% endif %}
  <li>{{ forloop.index }}. {{ item }}</li>
  {% if forloop.last %}</ul>{% endif %}
{% endfor %}

assign

You can create a variable or store a computed value.

{% assign greeting = "Hello" %}
{% assign full_name = name | append: " " | append: surname %}
{% assign price_with_tax = c_last_order_amount | times: 1.18 %}

Variables created with assign persist for the rest of the template, including after loops.

capture

You can capture a block of rendered content into a variable.

{% capture order_summary %}
  <b>{{ name }}</b> — {{ c_loyalty_tier | upcase }} member
{% endcapture %}

{{ order_summary }}

It is useful for building strings that are reused in multiple places, or for constructing a subject line.

{% capture subject %}
  {% if c_loyalty_tier == "gold" %}
    Your exclusive Gold offer is ready, {{ name }}
  {% else %}
    A special offer just for you, {{ name | default: "there" }}
  {% endif %}
{% endcapture %}

Subject: {{ subject | strip }}

increment / decrement

They are auto-counting variables, independent from assign.

increment starts at 0 and counts up.

{% increment counter %}   →  0
{% increment counter %}   →  1
{% increment counter %}   →  2

decrement starts at -1 and counts down.

{% decrement counter %}   →  -1
{% decrement counter %}   →  -2
{% decrement counter %}   →  -3

increment / decrement are entirely independent from assign. Using the same name for both does not cause a conflict, they maintain separate values.

{% assign x = 10 %}
{% increment x %}    →  0    (not 10)
{{ x }}              →  10   (assign variable unchanged)

comment

Everything inside is ignored and not rendered.

{% comment %}
  This will not appear in the output.
  {{ variables }} here are not processed either.
{% endcomment %}

raw

You can disable template processing for a block.

{% raw %}
  Use {{ variable }} and {% tag %} syntax in your templates.
{% endraw %}