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?
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://mcmap.net/q/408735/-can-you-use-jekyll-page-variables-in-a-layout
Is there any other way to do it?
Nope, your split
filter is the way to do it.
split
. Shopify docs on liquid array: docs.shopify.com/themes/liquid-documentation/basics/… –
Kapellmeister 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.If you put the array in the page frontmatter like:
---
my_array:
- one
- two
- three
---
I have tested that you could simply write it like so:
---
my_array: [one,two,three]
my_prime: [5,7,11,13,17,19]
---
Both of {{ page.my_array }}
and {{ page.my_prime }}
will output correctly.
© 2022 - 2024 — McMap. All rights reserved.