【问题标题】:How to create an array in a for loop in Liquid?如何在 Liquid 的 for 循环中创建数组?
【发布时间】:2017-05-06 22:08:48
【问题描述】:

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

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

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

有什么帮助吗?

【问题讨论】:

    标签: arrays liquid azure-api-management


    【解决方案1】:

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

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

    【讨论】:

    • Maroine,这是否按要求在 API 管理模板中工作?我试图做类似的事情,但看起来concat 不能使用
    • 如果 controllerName 本身不是一个数组(我认为它不会出现在 OP 的问题中),您需要在最后一个分配中使用 append 。您可以跳过第一次分配中的拆分。
    【解决方案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 %}
    
    

    【讨论】:

      【解决方案3】:

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

       {% assign controllersTmp = '' %}
      

      【讨论】:

        猜你喜欢
        • 2023-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 2018-06-11
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        相关资源
        最近更新 更多