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?
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
Here's another way to do it by first using capture
as a friendly way to assign newline-separated values to a variable and then converting that variable to an array with assign
and a few filters:
{% capture my_array %}
one
two
three
{% endcapture %}
{% assign my_array = my_array | strip | newline_to_br | strip_newlines | split: "<br />" %}
The filters do the following:
strip
removes the leading whitespace before one
and the trailing whitespace after three
.newline_to_br
replaces newlines with <br />
tags.strip_newlines
removes possible extraneous newlines.split
converts the string into an array, using <br />
as a separator.