55

我正在 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>

但到目前为止,我的任何想法都让我失望了。

4

3 回答 3

114

当您使用名为 的变量迭代哈希时hashhash[0]包含键并hash[1]包含每次迭代的值。

{% for link_hash in page.links %}
  {% for link in link_hash %}
    <a href="{{ link[1] }}">{{ link[0] }}</a>
  {% endfor %}
{% endfor %}
于 2011-11-28T23:29:27.517 回答
25

我会在 YAML 中这样定义它们:

links:
  demo: http://www.github.com/copperegg/mongo-scaling-demo

然后迭代:

{% for link in page.links %}
  <a href="{{ link[1] }}">{{ link[0] }}</a>
{% endfor %}
于 2014-05-24T07:36:20.493 回答
0
  {% for link in page.links %}
      {% for item in link %}
        <a href="{{ item[0] }}">{{ link[1] }}</a>
      {% endfor %}
    {% endfor %}

我有一个非常相似的问题,但我的变量中有多个项目,所以我使用了未记录的item变量并且它完成了工作。

于 2017-06-12T16:22:54.923 回答