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

I am 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 controllersTmpto get my array, but at this moment mine is controllersTmpempty.

Any help?

+4
source share
2 answers

You can directly create a new empty array controllersand concathave your controllerNameconvert to an array using a workaround split:''. The result is an array, without additional string manipulation.

{% assign controllers = '' | split: '' %}
{% for operation in menuItems %}
    {% assign controllerName = operation.Title | split: '_' | first | split: '' %}
    {% assign controllers = controllers | concat: controllerName %}
{% endfor %}
+9
source

you need to initialize the controllerTmp variables:

 {% assign controllersTmp = '' %}
0
source

Source: https://habr.com/ru/post/1664426/


All Articles