【问题标题】:Using Jekyll, how do you alter an array's contents using a for loop?使用 Jekyll,如何使用 for 循环更改数组的内容?
【发布时间】:2019-05-19 21:00:43
【问题描述】:

假设我的作用域中有一个数组 thingy.foo = ['abc', 'def']

我的目标是能够遍历 thingy.foo 中的所有项目并对其应用一些条件逻辑,覆盖数组中的现有项目......像这样:

{% for item in thingy.foo %}
  {% assign thingy.foo[forloop.index0] = site.data.lookups[item] | default: item %}
{% endfor %}

我正在做的项目有点无关紧要,我遇到问题的部分是更新数组中的项目。代码编译并运行。在循环中,我可以确认“查找”部分有效(如果我将其分配给t 并检查t,那么我会得到一个查找值,但thingy.foo[0] 仍然是原始值)。

是否可以在 Jekyll 中更新/覆盖数组?

(这是为了在 GitHub Pages 上使用,所以我不能使用自定义插件)。

【问题讨论】:

    标签: jekyll github-pages liquid


    【解决方案1】:

    看起来你不能改变现有的数组......但你可以循环初始数组并将项目改变成一个新数组,如下所示:

    {% assign newArray = '' | split: '' %}
    {% for item in thingy.foo %}
      {% assign newItem = site.data.lookups[item] | default: item %}
      {% assign newArray = newArray | push: newItem %}
    {% endfor %}
    

    newArray 现在包含来自thingy.foo 的更改项目列表。

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多