首页 文章

在液体模板中迭代哈希值

提问于
浏览
39

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

但到目前为止,我所有的想法都让我失望了 .

3 回答

  • 21

    当您使用名为 hash 的变量迭代哈希时, hash[0] 包含密钥, hash[1] 包含每次迭代的值 .

    {% for link_hash in page.links %}
      {% for link in link_hash %}
        <a href="{{ link[1] }}">{{ link[0] }}</a>
      {% endfor %}
    {% endfor %}
    
  • 92

    我会在YAML中定义它们:

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

    然后迭代:

    {% for link in page.links %}
      <a href="{{ link[1] }}">{{ link[0] }}</a>
    {% endfor %}
    
  • 0
    {% for link in page.links %}
          {% for item in link %}
            <a href="{{ item[0] }}">{{ link[1] }}</a>
          {% endfor %}
        {% endfor %}
    

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

相关问题