assign
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.Arrays support assignment by index, using integer lookups.
Code
Output
{% 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 }}
["one","two","three"]
Hashes support assignment by key, using string lookups.
Code
Output
{% assign x = hash %}
{% assign x["one"] = 1 %}
{% assign x["two"] = 2 %}
{% assign three = "three" %}
{% assign x[three] = 3 %}
{{ x | json }}
{"one":1,"two":2,"three":3}
Last modified 2yr ago