Accessing _data in Jekyll (loop in loop)

痞子三分冷 提交于 2019-12-01 19:46:00

YAML:

- title: "Projects"
  subcategories:
    - title: "project-sub1"
      items:
        - title: "project-sub1-item1"
          href: "#"
        - title: "project-sub1-item2"
          href: "#"
    - title: "project-sub2"
      items:
        - title: "project-sub2-item1"
          href: "#"
        - title: "project-sub2-item2"
          href: "#"

- title: "Support"
  subcategories:
   - title: "support-sub1"
     items:
      - title: "support-sub1-item1"
        href: "#"
      - title: "support-sub1-item2"
        href: "#"

Nested loops:

{% for entry in site.data.entries %}
  <h2>{{ entry.title }}</h2>
  {% for subcategory in entry.subcategories %}
    <h3>{{ subcategory.title }}</h3>
    <ul>
    {% for item in subcategory.items %}
      <li><a href="{{ item.href }}">{{ item.title }}</a></li>
    {% endfor %}
    </ul>
  {% endfor %}
{% endfor %}

Output:

<h2>Projects</h3>

  <h3>project-sub1</h3>
  <ul>
    <li><a href="#">project-sub1-item1</a></li>
    <li><a href="#">project-sub1-item2</a></li>
  </ul>

  <h3>project-sub2</h3>
  <ul>
    <li><a href="#">project-sub2-item1</a></li>
    <li><a href="#">project-sub2-item2</a></li>
  </ul>

<h2>Support</h3>

  <h3>support-sub1</h3>
  <ul>
    <li><a href="#">support-sub1-item1</a></li>
    <li><a href="#">support-sub1-item2</a></li>
  </ul>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!