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.

{% assign foo = "bar" %}
{% assign x = array %}
{% assign x["foo"] = foo %}

{% assign foo = "qux" %}

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.

{% assign x = array %}
{% assign x[0] = "one" %}
{% assign x[x.size] = "two" %}

{% assign the_third_zero_based_index = 2 %}
{% assign x[the_third_zero_based_index] = "three" %}

{{ x | json }}

Assigning into hashes

Hashes support assignment by key, using string lookups.

{% assign x = hash %}
{% assign x["one"] = 1 %}
{% assign x["two"] = 2 %}

{% assign three = "three" %}
{% assign x[three] = 3 %}

{{ x | json }}

Last updated