簡體   English   中英

如何在 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為空。

有什么幫助嗎?

您可以直接創建一個新的空數組controllersconcat它你controllerName轉換成使用解決方法數組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 %}

你必須初始化你的變量 controllersTmp :

 {% assign controllersTmp = '' %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM