assign
The assign tag is a native feature of Liquid. In Mechanic-flavored Liquid, the assign tag is extended to support assigning within arrays and hashes.
Assignment into arrays and hashes is always by value, never by reference.
"Assignment by value" means that the result of the assignment will never dynamically change.
1
{% assign foo = "bar" %}
2
{% assign x = array %}
3
{% assign x["foo"] = foo %}
4
​
5
{% assign foo = "qux" %}
Copied!
At the end of this example, x.foo still contains "bar", even though the value of the original foo variable has changed.

Assigning into arrays

Arrays support assignment by index, using integer lookups.
Code
Output
1
{% assign x = array %}
2
{% assign x[0] = "one" %}
3
{% assign x[x.size] = "two" %}
4
​
5
{% assign the_third_zero_based_index = 2 %}
6
{% assign x[the_third_zero_based_index] = "three" %}
7
​
8
{{ x | json }}
Copied!
1
["one","two","three"]
Copied!

Assigning into hashes

Hashes support assignment by key, using string lookups.
Code
Output
1
{% assign x = hash %}
2
{% assign x["one"] = 1 %}
3
{% assign x["two"] = 2 %}
4
​
5
{% assign three = "three" %}
6
{% assign x[three] = 3 %}
7
​
8
{{ x | json }}
Copied!
1
{"one":1,"two":2,"three":3}
Copied!
Last modified 6mo ago