Variable tags
You can use variable tags to create new Liquid variables.
assign
Creates a new named variable.
{% assign favorite_food = 'apples' %}
My favorite food is {{ favorite_food }}.
My favorite food is apples.
assigned
variables can be strings or booleans (true or false). Remember not to use quotation marks around the value if it is true
or false
:
{% assign first_time_visitor = true %}
{% if first_time_visitor == true %}
Welcome to the site!
{% endif %}
Welcome to the site!
capture
Captures the string inside of the opening and closing tags and assigns it to a variable. Variables that you create using capture
are stored as strings.
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 }}
I am 35 and my favorite food is pizza.
increment
Creates a new number variable, and increases its value by 1 every time increment
is called on the variable. The counter's initial value is 0.
Here, an increment
counter is used to create a unique numbered class for each list item:
<ul>
<li class="item-{% increment counter %}">apples</li>
<li class="item-{% increment counter %}">oranges</li>
<li class="item-{% increment counter %}">peaches</li>
<li class="item-{% increment counter %}">plums</li>
</ul>
<ul>
<li class="item-0">apples</li>
<li class="item-1">oranges</li>
<li class="item-2">peaches</li>
<li class="item-3">plums</li>
</ul>
decrement
Creates a new number variable, and decreases its value by 1 every time decrement
is called on the variable. The counter's initial value is -1.
{% decrement variable %}
{% decrement variable %}
{% decrement variable %}
-1
-2
-3
Updated about 3 years ago