迭代液体模板中的哈希值

发布于 2024-12-17 07:07:54 字数 572 浏览 5 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罗罗贝儿 2024-12-24 07:07:54

当您使用名为 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 %}

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 %}
江湖彼岸 2024-12-24 07:07:54

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

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

然后迭代:

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

I would define them like this in YAML:

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

And then iterate:

{% for link in page.links %}
  <a href="{{ link[1] }}">{{ link[0] }}</a>
{% endfor %}
北城半夏 2024-12-24 07:07:54
  {% for link in page.links %}
      {% for item in link %}
        <a href="{{ item[0] }}">{{ link[1] }}</a>
      {% endfor %}
    {% endfor %}

我遇到了非常相似的问题,但我的变量中有多个项目,因此我使用了未记录的 item 变量,它完成了这项工作。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文