Liquid for 循环在 Jekyll 中无法正常工作
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您之所以得到这些结果,是因为 Liquid 解析关联数组的方式 - 这就是
work_left
的本质。在每次迭代中,您都会得到两个项目:“键”和“值”。我警告您,在某些情况下这可能会给您带来问题。值得注意的是,项目出现的顺序无法保证 - isitgo 可能出现在 disco 之后。 (据我所知,这取决于您使用的 ruby 版本)。
如果您想确保始终以相同的顺序获取
work_left
的内容,则必须使用 关联数组列表而不是关联数组的关联数组,就像您所做的那样。其外观如下:然后是打印它们的代码:
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:Then the code to print them:
看起来用 for 循环创建的项目必须被视为数组。
因此,对于我的情况:
将输出返回的(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:
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
.