迭代液体模板中的哈希值
我正在 Jekyll 中编写一个使用 Liquid 的网站。
我希望页面的前部内容如下所示:
---
title: Designing algorithms that scale horizontally
speaker: Luke Ehresman, CopperEgg
category: notes.mongodallas.talks
links:
- demo: http://www.github.com/copperegg/mongo-scaling-demo
layout: talknotes
---
在 Liquid 中,YAML 的链接部分如下所示:
[{'demo' => 'http://www.github.com/copperegg/mongo-scaling-demo' }]
我希望能够迭代数组,执行如下操作:
<a href="{{ link.value }}">{{ link.key }}</a>
但是我有任何想法到目前为止已经让我失望了。
I'm writing a site in Jekyll, which uses Liquid.
I have front matter for pages that I'd like to look like this:
---
title: Designing algorithms that scale horizontally
speaker: Luke Ehresman, CopperEgg
category: notes.mongodallas.talks
links:
- demo: http://www.github.com/copperegg/mongo-scaling-demo
layout: talknotes
---
In Liquid, the links section of YAML comes through as:
[{'demo' => 'http://www.github.com/copperegg/mongo-scaling-demo' }]
I'd like to be able to iterate over the array, doing something like this:
<a href="{{ link.value }}">{{ link.key }}</a>
But any ideas I've had so far have failed me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用名为
hash
的变量迭代哈希时,hash[0]
包含键,hash[1]
包含每个值迭代。When you iterate over a hash using a variable called
hash
,hash[0]
contains the key andhash[1]
contains the value on each iteration.我会在 YAML 中这样定义它们:
然后迭代:
I would define them like this in YAML:
And then iterate:
我遇到了非常相似的问题,但我的变量中有多个项目,因此我使用了未记录的
item
变量,它完成了这项工作。I had a very similar issue, but I had multiple items in my variable so I used the undocumented
item
variable and it did the job.