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

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

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

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

我想拆分 controllersTmp 以获得我的数组,但此时我的 controllersTmp 是空的。

有什么帮助吗?

你必须初始化你的变量 controllersTmp :

 {% assign controllersTmp = '' %}

可以直接新建一个空数组controllersconcat to it your controllerName converted into an array using the workaround split:''。结果直接是一个数组,没有额外的字符串操作。

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

什么对我有用

{% 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 %}