遍历液体模板中的哈希 [英] Iterate over hashes in liquid templates

查看:62
本文介绍了遍历液体模板中的哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Jekyll编写一个使用Liquid的网站.

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
---

在Liquid中,YAML的链接部分通过以下方式显示:

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.

推荐答案

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

When you iterate over a hash using a variable called hash, hash[0] contains the key and hash[1] contains the value on each iteration.

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

这篇关于遍历液体模板中的哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆