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

时间:2016-12-21 13:09:53

标签: 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 %}

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

任何帮助?

3 个答案:

答案 0 :(得分:19)

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

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

答案 1 :(得分:2)

什么对我有用

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

答案 2 :(得分:0)

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

 {% assign controllersTmp = '' %}