Assign an array literal to a variable in Liquid Template

Stefano Ortisi picture Stefano Ortisi · Mar 31, 2014 · Viewed 14.3k times · Source

The only way i know to create an array from my liquid template is:

{% assign my_array = "one|two|three" | split: "|" %}

Is there any other way to do it?

Answer

Frontmatter

This is a good workaround, add to the top of your file:

---
my_array:
  - one
  - two
  - three
---

then use it as:

{{ page.my_array }}

Analogous for site wide site.data.my_array on the _config or under _data/some_file.yml.

Jekyll 3 update for layouts

If the frontmatter is that of a layout, you need to use:

{{ layout.style }}

instead. See: https://stackoverflow.com/a/37418818/895245