gpt4 book ai didi

jekyll - 在 Jekyll 中访问 _data (loop in loop)

转载 作者:行者123 更新时间:2023-12-03 14:19:36 28 4
gpt4 key购买 nike

鉴于此 YAML:

- maincategory:
title: "Projects"
subcategory:
title: "General"
item:
title: "Alpha"
item:
title: "Beta"

- maincategory:
title: "Support"
subcategory:
title: "General"
item:
title: "Something"
item:
title: "Else"

如果 Jekyll _data 文件被称为entries.yml,如何迭代这些数据?

到目前为止,我已经到了这里,但我不确定是否应该在子循环中继续引用 site.data 对象。也不确定这是否可能。
  {% for entry in site.data.entries %}
<h2>{{ entry.maincategory.title }}</h3>
{% for subcategory in site.data.entries.maincategories %}
<h3>{{ entry.maincategory.subcategory.title }}</h3>
<ul>
{% for item in site.data.entries.maincategory.subcategories %}
<li><a href="{{ item.href }}">{{ item.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}

需要明确的是,这就是我想要结束的地方(输出明智):
<!-- Loop over every main category -->
<h2>Main category title</h2>
<!-- Loop over every sub category within main category -->
<h3>Subcategory title</h3>
<ul>
<!-- Loop over every item in this subcategory -->
<li><a href="#">Item title</a>
</li>

最佳答案

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: "#"

嵌套循环:
{% 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 %}

输出:
<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>

关于jekyll - 在 Jekyll 中访问 _data (loop in loop),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21453960/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com