Tags contain logic that calculate or modify data, and they are defined using {% ... %}
Some tags be used to conditionally produce text. Many tags come in pairs: an opening tag, the content, and a matching closing tag whose name starts with end.
There are different types of tags:
- Variables store and reuse values
- Conditions show data only when something is true
- Loops repeat content for each item in an array
- Other tags include comments, raw text and the
liquidtag
Variables
A variable is shorthand for a value, which is convenient, because you can define it once and use it multiple times. The following tags create variables:
| Tag | Description |
|---|---|
assign |
Stores a value (or the result of filters) under a name |
capture |
Stores a block of content, including text, output and other tags, as a string |
Naming variables: use lower-case letters, numbers and underscores, such as print_run or full_title, and no spaces. Names are case-sensitive, so Title and title are different variables.
assign
The syntax is:
1
{% assign variable_name = value %}
Stores a value under a name. The value can be a string, a number, a boolean, another variable, or any of these passed through one or more filters.
Examples
1
2
{% assign title = "The Hobbit" -%}
Introducing {{ title }}
results in:
1
Introducing The Hobbit
Use filters to prepare a value:
1
2
{% assign title = "The Hobbit" | upcase | append: "!" -%}
{{ title }}
results in:
1
THE HOBBIT!
Copy a property out of an object to give it a shorter name:
1
{% assign city = person.default_address.city %}
Turn a string into an array:
1
{% assign currencies = "GBP,USD,EUR" | split: "," %}
Build up a running total:
1
2
3
4
5
{% assign total = 0 %}
{% for n in numbers %}
{% assign total = total | plus: n %}
{% endfor %}
{{ total }}
With the numbers 1, 2 and 3, this prints 6.
capture
The syntax is:
1
2
3
{% capture variable_name %}
...content...
{% endcapture %}
capture stores everything between the opening and closing tags as a string. The content can include text, output, filters and other tags. Nothing is printed at the point of capture; you print the result later by using the variable.
Examples
Joining text together:
1
2
{% capture full_name %}{{ first_name }} {{ last_name }}{% endcapture %}
Written by {{ full_name }}.
1
Written by Sam Jones.
Store the outcome of using conditional if-else tags:
1
2
3
4
{% capture availability %}
{%- if copies > 0 -%}In stock{%- else -%}Out of stock{%- endif -%}
{% endcapture %}
Status: {{ availability }}
Conditions
Conditions let a template show different content depending on the data.
| Tag | Description |
|---|---|
if |
Shows content only when a condition is true |
elsif |
Adds another condition to an if, tried if the earlier ones are not true |
else |
Shows content when none of the conditions are true |
unless |
The opposite of if: shows content only when a condition is not true |
case / when |
Compares one value against several possibilities |
if, elsif and else
The syntax is:
1
2
3
4
5
6
7
{% if condition %}
...shown when the condition is true...
{% elsif other_condition %}
...shown when the first was not true but this one is...
{% else %}
...shown when none of the above were true...
{% endif %}
Only the if and the closing endif are required. You can add any number of elsif sections, and one else at the end.
Liquid checks the conditions from top to bottom and shows the content of the first one that is true.
Examples
A single condition:
1
2
3
{% if work.subtitle != blank %}
<h2>{{ work.subtitle }}</h2>
{% endif %}
Several conditions, with a fallback:
1
2
3
4
5
6
7
{% if copies > 1000 %}
Large print run
{% elsif copies > 100 %}
Medium print run
{% else %}
Small print run
{% endif %}
With copies set to 250, this prints Medium print run.
unless, and else
The syntax is:
1
2
3
{% unless condition %}
...shown when the condition is NOT true...
{% endunless %}
Examples
1
2
3
{% unless status == "draft" %}
Published
{% endunless %}
1
2
3
4
5
{% unless copies > 0 %}
Out of stock
{% else %}
In stock
{% endunless %}
case, when and else
The syntax is:
1
2
3
4
5
6
7
8
{% case value %}
{% when option_1 %}
...
{% when option_2, option_3 %}
...
{% else %}
...
{% endcase %}
case takes a single value and compares it for equality against each when in turn.
Example
1
2
3
4
5
{% case status %}
{% when "draft" %}Not yet published
{% when "live", "scheduled" %}Visible or coming soon
{% else %}Unknown status
{% endcase %}
With status set to "scheduled", this prints Visible or coming soon.
Liquid supports overlapping options. Here, "hardback" matches both when lines:
1
2
3
4
{% case format %}
{% when "hardback" %}Hardback
{% when "hardback", "paperback" %}Print edition
{% endcase %}
With format set to "hardback", this prints both Hardback and Print edition.
Loops
A loop repeats a block of content once for each item in an array, or once for each number in a range.
| Tag | Description |
|---|---|
for |
Repeats content for each item in an array or range |
break |
Stops the loop early |
continue |
Skips the rest of the current pass and moves on to the next item |
tablerow |
Repeats content for each item, laying the results out as HTML table rows and cells |
Inside a for loop you also get a helper called forloop, which tells you where you are in the loop (first item, last item, item number and so on).
In the examples below, subjects is an array holding: crime, biography, romance, history, horror.
for
Syntax
1
2
3
4
5
{% for item in array %}
...content, repeated for each item...
{% else %}
...shown if there are no items...
{% endfor %}
item is a name you choose. On each pass it holds the current item; use it like any other variable. The else section is optional.
Example
1
{% for subject in subjects %} {{ subject }} {% endfor %}
1
crime biography romance history horror
When there is nothing to loop over
If the array is empty or has no value (nil), the loop doesn’t run, and the else section (if you have one) is shown instead:
1
2
3
4
5
{% for subject in subjects %}
<li>{{ subject }}</li>
{% else %}
<li>No subjects available.</li>
{% endfor %}
Options
Add options after the array name to control which items are used.
| Option | What it does | Example | Result |
|---|---|---|---|
limit: n |
Stops after n items |
{% for subject in subjects limit: 2 %} |
crime biography |
offset: n |
Skips the first n items |
{% for subject in subjects offset: 3 %} |
history horror |
limit and offset together |
Skips, then stops | {% for subject in subjects limit: 2 offset: 1 %} |
biography romance |
reversed |
Goes through the items last to first | {% for subject in subjects reversed %} |
horror history romance biography crime |
The forloop helper
Inside any for loop, forloop gives you information about the current pass:
| Property | Description |
|---|---|
forloop.index |
The item number, starting at 1 |
forloop.index0 |
The item number, starting at 0 |
forloop.rindex |
The item number counting back from the end: the last item is 1 |
forloop.rindex0 |
The same, but the last item is 0 |
forloop.first |
true on the first pass, otherwise false |
forloop.last |
true on the last pass, otherwise false |
forloop.length |
How many times the loop will run, after limit and offset are applied |
forloop.parentloop |
The forloop of an enclosing loop (nothing if there isn’t one) |
Examples
A numbered list:
1
{% for subject in subjects limit: 3 %}{{ forloop.index }}. {{ subject }} {% endfor %}
1
1. crime 2. biography 3. romance
break and continue
Syntax
1
2
{% break %}
{% continue %}
Both are used inside a for loop, normally inside an if.
breakstops the loop completely.continueskips the rest of the current pass and moves straight to the next item.
Examples
1
{% for i in (1..5) %}{% if i == 4 %}{% break %}{% endif %}{{ i }} {% endfor %}
results in
1
1 2 3
1
{% for i in (1..5) %}{% if i == 4 %}{% continue %}{% endif %}{{ i }} {% endfor %}
results in
1
1 2 3 5
tablerow
Syntax
1
2
3
4
5
<table>
{% tablerow item in array cols: 2 %}
...content for each cell...
{% endtablerow %}
</table>
tablerow is like for, but emits the HTML for table rows (<tr>) and cells (<td>). You have to put in the surrounding <table> and <\table> tags yourself.
Example
1
2
3
4
5
<table>
{% tablerow subject in subjects cols: 2 %}
{{ subject }}
{% endtablerow %}
</table>
The result is a table with two columns:
1
2
3
4
5
<table>
<tr class="row1"><td class="col1">crime</td><td class="col2">biography</td></tr>
<tr class="row2"><td class="col1">romance</td><td class="col2">history</td></tr>
<tr class="row3"><td class="col1">horror</td></tr>
</table>
Options
| Option | Description |
|---|---|
cols: n |
The number of cells per row |
limit: n |
Stops after n items |
offset: n |
Skips the first n items |
You can also loop over a range, as with for: {% tablerow i in (1..6) cols: 3 %}.
Inside tablerow you get tablerowloop, which has all the same properties as forloop (except parentloop), plus these:
| Property | Description |
|---|---|
tablerowloop.col |
The current column number, starting at 1 |
tablerowloop.col0 |
The current column number, starting at 0 |
tablerowloop.col_first |
true if this is the first cell in its row |
tablerowloop.col_last |
true if this is the last cell in its row |
tablerowloop.row |
The current row number, starting at 1 |
Examples
Show a list only if there is something to list:
1
2
3
4
5
{% if subjects != blank %}
<ul>
{% for subject in subjects %}<li>{{ subject }}</li>{% endfor %}
</ul>
{% endif %}
Show the first few items and a count of the rest:
1
{% for subject in subjects limit: 3 %}{{ subject }}{% unless forloop.last %}, {% endunless %}{% endfor %}{% if subjects.size > 3 %} and {{ subjects.size | minus: 3 }} more{% endif %}
1
crime, biography, romance and 2 more
Keep a running total:
1
2
3
4
5
{% assign total = 0 %}
{% for n in numbers %}
{% assign total = total | plus: n %}
{% endfor %}
{{ total }}
Other tags
Comments: comment and #
Comments are notes for people reading the template. They never appear in the output.
Block comment
1
2
3
{% comment %}
Check these dates.
{% endcomment %}
Everything between comment and endcomment is ignored, including any Liquid inside it.
Inline comment
1
{% # This heading only shows for admin %}
1
2
3
4
{%
# First line of the note
# Second line of the note
%}
raw
Syntax
1
2
3
{% raw %}
...content printed exactly as written...
{% endraw %}
Everything between raw and endraw is printed literally.
Example
1
2
3
{% raw %}
Write {{ name }} to insert the author's name.
{% endraw %}
1
Write {{ name }} to insert the author's name.
liquid
Syntax
1
2
3
4
5
{% liquid
tag_1
tag_2
...
%}
Omits the need for {% %} brackets on every line.
Example
1
2
3
4
5
6
{% liquid
assign name = "Emma"
if name == "Emma"
echo "Hello, Emma"
endif
%}
1
Hello, Emma
A loop. The {{ }} brackets aren’t allowed inside a liquid tag, so use echo value:
1
2
3
4
5
6
7
8
{% liquid
for subject in subjects
echo subject | upcase
unless forloop.last
echo ", "
endunless
endfor
%}
1
CRIME, BIOGRAPHY, ROMANCE, HISTORY, HORROR
Consonance-specific tags
These tags have been written for Consonance specifically and are not part of the standard Liquid language
markup
Helps you to generate nested XML or HTML markup.
The values passed to markup are split at the first space character, with the string before the space being used as the
opening and closing tags, and any subsequent text being used as attributes in the opening tag.
1
2
3
4
5
6
7
8
9
10
11
{% markup Work language="en" %}
{% markup Title%}{{ work.title }}{% endmarkup %}
{% markup Subtitle %}{{ work.subtitle }}{% endmarkup %}
{% markup Products %}
{%- for product in work.products %}
{% markup Product %}
{% markup ProductForm %}{{ product.product_form_onix2_code }}{% endmarkup %}
{% endmarkup %}
{%- endfor%}
{% endmarkup %}
{% endmarkup %}
1
2
3
4
5
6
7
8
9
10
11
12
<Work language="en">
<Title>The Hobbit</Title>
<Subtitle>There and Back Again</Subtitle>
<Products>
<Product>
<ProductForm>BB</ProductForm>
</Product>
<Product>
<ProductForm>BC</ProductForm>
</Product>
</Products>
</Work>
Do not confuse the markup tag with the markup filter. The tag is used to wrap other elements in opening and closing tags, and the filter modifies a string to make it legal to include in HTML and XML. However, they will commonly be used together.
1
2
3
{% markup Work variant='monograph' %}
{{ work.title | markup: "Title type='main'"}}
{% endmarkup %}
1
2
3
<Work variant="monograph">
<Title type='main'>The Hobbit</Title>
</Work>
xml_instruct
Adds an XML instruction, which should be used to start all XML files.
Syntax
1
{% xml_instruct xml version="1.0" encoding="UTF-8"%}
1
<?xml version="1.0" encoding="UTF-8"?>