Liquid implements business-logic independent tags. This section contains the specification and demos for all the tags implemented by WISMOlabs implementation of Liquid.
There’re a dozen of tags supported by Liquid. These tags can be categorized into these groups:
Category | Purpose | Tags |
Iteration | iterate over a collection | or, cycle, tablerow |
Control Flow | control the execution branch of template rendering | if, unless, elsif, else, case, when |
Variable | define and alter variables | assign, increment, decrement, capture, echo |
Language | temporarily disable Liquid syntax | # (inline comment), raw, comment, liquid |
Anything inside an inline comment tag will not be printed.
{% # this is an inline comment %}
But every line must start with a '#'.
{%
# this is a comment
# that spans multiple lines
%}
Anything inside an inline comment tag will not be printed.
But every line must start with a '#'.
{% liquid
# required args
assign product = collection.products.first
# optional args
assign should_show_border = should_show_border | default: true
assign should_highlight = should_highlight | default: false
%}
{%- # {% echo 'Welcome to WISMOlabs!' %}
-%}
{% comment %}{% echo 'Welcome to WISMOlabs!' %}{% endcomment %}
-%}
{% assign my_variable = false %} {% if my_variable != true %} This statement is valid. {% endif %}Output
This statement is valid.Wrap a variable value in quotations " to save it as a string.
{% assign foo = "bar" %} {{ foo }}Output
bar
{% capture my_variable %}I am being captured.{% endcapture %} {{ my_variable }}Output
I am being captured.Using capture, you can create complex strings using other variables created with assign:
{% assign favorite_food = "pizza" %} {% assign age = 35 %} {% capture about_me %} I am {{ age }} and my favorite food is {{ favorite_food }}. {% endcapture %} {{ about_me }}Output
I am 35 and my favourite food is pizza.
{% assign handle = "cake" %} {% case handle %} {% when "cake" %} This is a cake {% when "cookie", "biscuit" %} This is a cookie {% else %} This is neither a cake nor a cookie {% endcase %}Output
This is a cake
Anything you put between {% comment %} and {% endcomment %} tags is turned into a comment.Output
Anything you put between tags is turned into a comment.
{% cycle "one", "two", "three" %} {% cycle "one", "two", "three" %} {% cycle "one", "two", "three" %} {% cycle "one", "two", "three" %}Output
one two three oneUses for cycle include: applying odd/even classes to rows in a table applying a unique class to the last product thumbnail in a row
{% cycle "first": "one", "two", "three" %} {% cycle "second": "one", "two", "three" %} {% cycle "second": "one", "two", "three" %} {% cycle "first": "one", "two", "three" %}Output
one one two two
{% decrement variable %} {% decrement variable %} {% decrement variable %}Output
-1 -2 -3Like , variables declared inside decrement are independent from variables created through or .
{% assign username = 'Bob' %} {% echo username | append: ", welcome to WISMOlabs Liquid!" | capitalize %}Output
Bob, welcome to WISMOlabs Liquid!
{% for product in collection.products %} {{ product.title }} {% endfor %}Output
hat shirt pantsFor loops can iterate over arrays, hashes, and . When iterating a hash, item[0] contains the key, and item[1] contains the value:
{% for item in hash %} * {{ item[0] }}: {{ item[1] }} {% endfor %}Output
* key1: value1 * key2: value2elseSpecifies a fallback case for a for loop which will run if the loop has zero length.
{% for product in collection.products %} {{ product.title }} {% else %} The collection is empty. {% endfor %}Output
The collection is empty.breakCauses the loop to stop iterating when it encounters the break tag.
{% for i in (1..5) %} {%- if i == 4 -%} {% break %} {%- else -%} {{ i }} {%- endif -%} {% endfor %}Output
123continueCauses the loop to skip the current iteration when it encounters the continue tag.
{% for i in (1..5) %} {%- if i == 4 -%} {%- continue -%} {%- else -%} {{ i }} {%- endif -%} {% endfor %}Output
1235forloopThere’s a forloop object available inside for loops. It’s used to indicate the current state of for loop. The forloop.first, forloop.last and forloop.length property:
{% for i in (1..5) %} {%- if forloop.first == true -%} First {%- elsif forloop.last == true -%} Last {%- else -%} {{ forloop.length }} {%- endif %} {% endfor -%}Output
First 5 5 5 LastThe forloop.index, forloop.index0, forloop.rindex and forloop.rindex0 property:
index index0 rindex rindex0 {% for i in (1..5) %} {{- forloop.index }} {{ forloop.index0 }} {{ forloop.rindex }} {{ forloop.rindex0 }} {% endfor -%}Output
index index0 rindex rindex0 1 0 5 4 2 1 4 3 3 2 3 2 4 3 2 1 5 4 1 0
<!-- for array = [1,2,3,4,5,6] --> {% for item in array limit:2 %} {{- item -}} {% endfor %}Output
12offsetBegins the loop at the specified index.
<!-- for array = [1,2,3,4,5,6] --> {% for item in array offset:2 %} {{- item -}} {% endfor %}Output
3456offset:continue Offset value can be continue to continue previous loop. For example:
<!-- for array = [1,2,3,4,5,6] --> {% for item in array limit:2 %} {{- item -}} {% endfor%} {% for item in array offset:continue %} {{- item -}} {% endfor%}Output
12 3456For the same variable name ("item" in this case) and same collection ("array" in this case), there’s one position record. That means you can start a new loop with a different variable name:
<!-- for array = [1,2,3,4,5,6] --> {% for item in array limit:2 %} {{- item -}} {% endfor%} {% for item2 in array offset:continue %} {{- item2 -}} {% endfor%}Output
12 123456rangeDefines a range of numbers to loop through. The range can be defined by both literal and variable numbers.
{% for i in (3..5) %} {{- i -}} {% endfor-%} {% assign num = 4 %} {% for i in (1..num) %} {{- i -}} {% endfor %}Output
345 1234reversedReverses the order of the loop. Note that this flag’s spelling is different from the filter reverse.
<!-- if array = [1,2,3,4,5,6] --> {% for item in array reversed %} {{ item }} {% endfor %}Output
6 5 4 3 2 1When used with additional parameters, order is important. Leading with reversed reverses the order of the loop before executing the other parameters.
{% for i in (1..8) reversed limit: 4 %} {{ i }} {% endfor %}Output
8 7 6 5Input
{% for i in (1..8) limit: 4 reversed %} {{ i }} {% endfor %}Output
4 3 2 1
{% if product.title == "Awesome Shoes" %} These shoes are awesome! {% endif %}Output
These shoes are awesome!
<!-- If customer.name = "anonymous" --> {% if customer.name == "kevin" %} Hey Kevin! {% elsif customer.name == "anonymous" %} Hey Anonymous! {% else %} Hi Stranger! {% endif %}Output
Hey Anonymous!
{% increment my_counter %} {% increment my_counter %} {% increment my_counter %}Output
0 1 2Variables created through the increment tag are independent from variables created through or . In the example below, a variable named “var” is created through assign. The increment tag is then used several times on a variable with the same name. Note that the increment tag does not affect the value of “var” that was created through assign.
{% assign var = 10 %} {% increment var %} {% increment var %} {% increment var %} {{ var }}Output
0 1 2 10
{% liquid assign names = 'Bob, Sally' | split: ', ' for name in names echo 'Hello, ' | append: name unless forloop.last echo ', ' endunless endfor %}Output
Hello, Bob, Hello Sally
{% raw %} In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not. {% endraw %}Output
In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.
<table> {% tablerow product in collection.products %} {{ product.title }} {% endtablerow %} </table>Output
<table> <tr class="row1"> <td class="col1"> Cool Shirt </td> <td class="col2"> Alien Poster </td> <td class="col3"> Batman Poster </td> <td class="col4"> Bullseye Shirt </td> <td class="col5"> Another Classic Vinyl </td> <td class="col6"> Awesome Jeans </td> </tr> </table>
{% tablerow product in collection.products cols:2 %} {{ product.title }} {% endtablerow %}Output
<table> <tr class="row1"> <td class="col1"> Cool Shirt </td> <td class="col2"> Alien Poster </td> </tr> <tr class="row2"> <td class="col1"> Batman Poster </td> <td class="col2"> Bullseye Shirt </td> </tr> <tr class="row3"> <td class="col1"> Another Classic Vinyl </td> <td class="col2"> Awesome Jeans </td> </tr> </table>limitExits the tablerow after a specific index.
{% tablerow product in collection.products cols:2 limit:3 %} {{ product.title }} {% endtablerow %}offsetStarts the tablerow after a specific index.
{% tablerow product in collection.products cols:2 offset:3 %} {{ product.title }} {% endtablerow %}rangeDefines a range of numbers to loop through. The range can be defined by both literal and variable numbers.
<!--variable number example--> {% assign num = 4 %} <table> {% tablerow i in (1..num) %} {{ i }} {% endtablerow %} </table> <!--literal number example--> <table> {% tablerow i in (3..5) %} {{ i }} {% endtablerow %} </table>
{% unless product.title == "Awesome Shoes" %} These shoes are not awesome. {% endunless %}Output
These shoes are not awesome.This would be the equivalent of doing the following:
{% if product.title != "Awesome Shoes" %} These shoes are not awesome. {% endif %}