Shopify(液体):预期语法错误 close_square 但发现逗号

Shopify (Liquid): Syntax error expected close_square but found comma

我在 Shopify (Liquid) 中创建数组时出现错误,

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

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

无法在液体中创建这样的数组。

相反,您可以使用 split 过滤器从字符串创建数组。

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

您还可以创建一个空数组并使用 push 过滤器

提供给它
{% 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>