Iterate over hashes in liquid templates

后端 未结 3 2023
别那么骄傲
别那么骄傲 2020-11-29 22:34

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 tha         


        
相关标签:
3条回答
  • 2020-11-29 22:34
      {% 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.

    0 讨论(0)
  • 2020-11-29 22:53

    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 %}
    
    0 讨论(0)
  • 2020-11-29 22:58

    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 %}
    
    0 讨论(0)
提交回复
热议问题