How to create an array in a for loop in Liquid?

2019-04-07 10:57发布

I'm trying to create an array from a list of objects using Liquid syntax:

{% for operation in menuItems %}
      {% assign words1 = operation.Title | split: '_' %}
      {% assign controllerName = words1 | first %}
      {% assign controllersTmp = controllersTmp | append: '_' | append: controllerName %}
{% endfor %}

I want to split the controllersTmp to get my array, but at this point my controllersTmp is empty.

Any help ?

2条回答
Emotional °昔
2楼-- · 2019-04-07 11:38

you have to init your variable controllersTmp :

 {% assign controllersTmp = '' %}
查看更多
淡お忘
3楼-- · 2019-04-07 11:53

You can directly create a new empty array controllers and concat to it your controllerName converted into an array using the workaround split:''. The result is directly an array, without the extra string manipulations.

{% assign controllers = '' | split: '' %}
{% for operation in menuItems %}
    {% assign controllerName = operation.Title | split: '_' | first | split: '' %}
    {% assign controllers = controllers | concat: controllerName %}
{% endfor %}
查看更多
登录 后发表回答