Mechanic's Liquid implementation includes support for mutable arrays and hashes, via the assign tag.

To facilitate this, we introduce two new keyword literals:

  • hash  – {} 
  • array  – [] 

These make it easy to initialize your data structures. Learn more about Mechanic's keyword literals.

Usage notes

  • You may assign within an array by index, using integer lookups.
  • You may assign within a hash by key, using string lookups.
  • Assignment within hashes and arrays is always by value, not by reference.

Examples

{% assign foo = array %}
{{ foo | json }}
=> []

{% assign foo[0] = "bar" %}
{{ foo | json }}
=> ["bar"]

{% assign baz = "foo,bar" | split: "," %}
{{ baz | json }}
=> ["foo","bar"]

{% assign baz[2] = "baz" %}
{{ baz | json }}
=> ["foo","bar","baz"]
{% assign foo = hash %}
{{ foo | json }}
=> {}

{% assign foo["bar"] = "baz" %}
{{ foo | json }}
=> {"bar":"baz"}

{% assign baz = '{"qux": "quux"}' | parse_json %}
{% assign foo["bar"] = baz %}
{{ foo | json }}
=> {"bar":{"qux":"quux"}}
Did this answer your question?