Variable Tags are used to create new Liquid variables.
Creates a new variable.
{% assign my_variable = false %}
{% if my_variable != true %}
This statement is valid.
{% endif %}
This statement is valid.
Use quotations " "
to save the variable as a string.
{% assign foo = "bar" %}
{{ foo }}
bar
Captures the string inside of the opening and closing tags and assigns it to a variable. Variables created through capture
are strings.
{% capture my_variable %}I am being captured.{% endcapture %}
{{ my_variable }}
I am being captured.
Creates a new number variable, and increases its value by one every time it is called. The initial value is 0.
{% increment variable %}
{% increment variable %}
{% increment variable %}
0
1
2
Variables created through the increment
tag are independent from variables created through assign
or capture
.
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. However, 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 }}
0
1
2
10
Creates a new number variable, and decreases its value by one every time it is called. The initial value is -1.
{% decrement variable %}
{% decrement variable %}
{% decrement variable %}
-1
-2
-3
Like increment, variables declared inside decrement
are independent from variables created through assign
or capture
.