Liquid for 循环在 Jekyll 中无法正常工作

发布于 2024-12-27 06:31:55 字数 933 浏览 4 评论 0原文

我在 Jekyll 中使用液体 for 循环来显示基于此 YAML 结构的页面内容:

work_left:
  isitgo:
    image: /images/isitgo.png
    caption: isitgoonair.net homepage
    description: a website
  disko:
    image: /images/disko.png
    caption: Disko
    description: a website
work_right:
  qfi:
    image: /images/qfi.png
    caption: qfi.im
    description: a website

这是 for 循环:

{% for item in page.work_left %}
    {{ item.image }}
{% endfor %}

item.image 不会生成字符串 /images/isitgo。 png/images/disko.png 正在输出。
如果我只是执行 {{ item }},结果如下:

isitgo
{
    "image"=>"/images/isitgo.png",
    "caption"=>"isitgoonair.net homepage",
    "description"=>"an awesome website i made"
}

disko
{
    "image"=>"/images/disko.png",
    "caption"=>"Disko",
    "description"=>"that site"
}

是什么导致了这个?

I'm using a liquid for loop in Jekyll to display page content based on this YAML structure:

work_left:
  isitgo:
    image: /images/isitgo.png
    caption: isitgoonair.net homepage
    description: a website
  disko:
    image: /images/disko.png
    caption: Disko
    description: a website
work_right:
  qfi:
    image: /images/qfi.png
    caption: qfi.im
    description: a website

This is the for loop:

{% for item in page.work_left %}
    {{ item.image }}
{% endfor %}

item.image will not result into the strings /images/isitgo.png and /images/disko.png being output.
If instead I just do {{ item }}, here's the result:

isitgo
{
    "image"=>"/images/isitgo.png",
    "caption"=>"isitgoonair.net homepage",
    "description"=>"an awesome website i made"
}

disko
{
    "image"=>"/images/disko.png",
    "caption"=>"Disko",
    "description"=>"that site"
}

What's causing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2025-01-03 06:31:55

您之所以得到这些结果,是因为 Liquid 解析关联数组的方式 - 这就是 work_left 的本质。在每次迭代中,您都会得到两个项目:“键”和“值”。

我警告您,在某些情况下这可能会给您带来问题。值得注意的是,项目出现的顺序无法保证 - isitgo 可能出现在 disco 之后。 (据我所知,这取决于您使用的 ruby​​ 版本)。

如果您想确保始终以相同的顺序获取 work_left 的内容,则必须使用 关联数组列表而不是关联数组的关联数组,就像您所做的那样。其外观如下:

work_left:
  - name: isitgo
    image: /images/isitgo.png
    caption: isitgoonair.net homepage
    description: a website
  - name: disko
    image: /images/disko.png
    caption: Disko
    description: a website
work_right:
  - name: qfi
    image: /images/qfi.png
    caption: qfi.im
    description: a website

然后是打印它们的代码:

{% for item in page.work_left %}
  {{ item.name }}
  {{ item.image }}
{% endfor %}

You are getting those results because of the way liquid parses associative arrays - which is what work_left is. On each iteration you get two items: the "key" and the "value".

I warn you that in some occasions this can give you problems. Notably, the order in which the items will appear is not guaranteed - isitgo could appear after disko. (This depends on the version of ruby you are using, as far as I know).

If you want to make sure that you always get the contents of work_left in the same order, you must use list of associative arrays instead of an associative array of associative array, as you where doing. Here's how it would look:

work_left:
  - name: isitgo
    image: /images/isitgo.png
    caption: isitgoonair.net homepage
    description: a website
  - name: disko
    image: /images/disko.png
    caption: Disko
    description: a website
work_right:
  - name: qfi
    image: /images/qfi.png
    caption: qfi.im
    description: a website

Then the code to print them:

{% for item in page.work_left %}
  {{ item.name }}
  {{ item.image }}
{% endfor %}
三岁铭 2025-01-03 06:31:55

看起来用 for 循环创建的项目必须被视为数组。

因此,对于我的情况:

{% for item in page.work_left %}
     {{ item[0] }}
{% endfor %}

将输出返回的(2 项)数组的第一个元素,它是一个字符串:isitgo disco{{ item[1].image }} 将访问返回数组的第二项(一个变量)的 .image 属性,并且它将显示 / images/isitgo.png /images/disko.png

It appears that the item created with a for loop has to be treated as an array.

So for my case:

{% for item in page.work_left %}
     {{ item[0] }}
{% endfor %}

Would output the first element of the returned (2-item) array, which is a string: isitgo disko. {{ item[1].image }} would access the .image property of the second item of the returned array, a variable, and it would display /images/isitgo.png /images/disko.png.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文