Variable

Variable tags create new Liquid variables.

assign

Creates a new variable.

Input

{% 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.

Input

{% assign foo = "bar" %}
{{ foo }}

Output

bar

capture

Captures the string inside of the opening and closing tags and assigns it to a variable. Variables created through capture are strings.

Input

{% 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:

Input

{% 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.

increment

Creates a new number variable, and increases its value by one every time it is called. The initial value is 0.

Input

{% increment my_counter %}
{% increment my_counter %}
{% increment my_counter %}

Output

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. Note that the increment tag does not affect the value of “var” that was created through assign.

Input

{% assign var = 10 %}
{% increment var %}
{% increment var %}
{% increment var %}
{{ var }}

Output

0
1
2
10

decrement

Creates a new number variable, and decreases its value by one every time it is called. The initial value is -1.

Input

{% decrement variable %}
{% decrement variable %}
{% decrement variable %}

Output

-1
-2
-3

Like increment, variables declared inside decrement are independent from variables created through assign or capture.

© 2005, 2006 Tobias Luetke
Licensed under the MIT License.
https://shopify.github.io/liquid/tags/variable/