Ad

Shopify (Liquid): Syntax Error Expected Close_square But Found Comma

- 1 answer

I am creating an array in Shopify (Liquid) and I get an error,

            {% assign numbers = [   
                                    "One", 
                                    "TWo", 
                                    "three",
                                    "bla"                                   
                                 ] 
            %}

Line 126 — Liquid syntax error: Expected close_square but found comma in "{{[ "One","TWo", "three","bla" ] }}"

Ad

Answer

There is no way to create an array like this in liquid.

Instead, you can use the split filter to create an array from a string.

{% assign numbers = "one,two,three,four" | split: "," %}
<pre>{{ numbers | inspect }}</pre>

You can also create an empty array and feed it with the push filter

{% comment %} +++ Creates an empty array +++ {% endcomment %}
{% assign numbers = "" | split: "" %}
<pre>{{ numbers | inspect }}</pre>
{% comment %} +++ Feed the beast +++ {% endcomment %}
{% assign numbers = numbers | push: "one" %}
<pre>{{ numbers | inspect }}</pre>
{% assign numbers = numbers | push: "two" %}
<pre>{{ numbers | inspect }}</pre>
Ad
source: stackoverflow.com
Ad