如何在Liquid中的for循环中创建数组?

Mar*_*lah 10 arrays liquid azure-api-management

我正在尝试使用Liquid语法从对象列表中创建一个数组:

{% for operation in menuItems %}
      {% assign words1 = operation.Title | split: '_' %}
      {% assign controllerName = words1 | first %}
      {% assign controllersTmp = controllersTmp | append: '_' | append: controllerName %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我想分裂controllersTmp得到我的数组,但此时我的controllersTmp是空的.

有帮助吗?

jrb*_*ard 20

您可以直接创建一个新的空数组,controllers并使用该变通方法concat将其controllerName转换为数组split:''.结果直接是一个数组,没有额外的字符串操作.

{% assign controllers = '' | split: '' %}
{% for operation in menuItems %}
    {% assign controllerName = operation.Title | split: '_' | first | split: '' %}
    {% assign controllers = controllers | concat: controllerName %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)


Ale*_*nen 6

什么对我有用

{% assign otherarticles = "" | split: ',' %}
{% assign software_engineering = "" | split: ',' %}

{% for file in site.static_files %}
  {% if file.extname == ".html" %}
    {% if file.path contains "software_engineering" %}
       {% assign software_engineering = software_engineering | push: file %}
    {% else %}
      {% assign otherarticles = otherarticles | push: file %}
    {% endif %}
  {% endif %}
{% endfor %}

Run Code Online (Sandbox Code Playgroud)