How to create an array in Liquid?

question liquid
Ram Patra Published on August 11, 2020
Liquid Version - 4.0.3

The official doc of Liquid doesn’t tell us a way to create an array except this:

{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}

{% for member in beatles %}
  {{ member }}
{% endfor %}

But what if you want to create an array of objects? Well, there’s a trick. First, create an empty array using split and then add your object to the array using push.

{% assign posts = "" | split: "," %}
{% assign posts = posts | push: post %}
Ram Patra Published on August 11, 2020
Image placeholder

Keep reading

If this article was helpful, others might be too

question liquid jekyll August 11, 2020 How to escape Liquid template code in a Jekyll website?

We know that Jekyll uses Liquid as its templating language. This means all your Liquid code will be executed by Jekyllby default.

question liquid August 11, 2020 How to check if a string is blank in Liquid?

I read the official docs of Liquid but couldn’t findthe info that I was looking for.