在Jekyll中访问_data(循环循环) [英] Accessing _data in Jekyll (loop in loop)
本文介绍了在Jekyll中访问_data(循环循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于此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文件称为entrys.yml,将如何遍历此数据?
How would one iterate over this data if the Jekyll _data file is called entries.yml?
到目前为止,我已经到了这里,但是我不确定是否应该继续在子循环中引用site.data对象.也不确定这是否有可能.
So far I've gotten here but I'm not sure whether I should keep referencing the site.data oject in sub-loops. Also not sure whether this is even possible.
{% 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 %}
要清楚,这是我要结束的地方(明智的输出方式):
To be clear, this is where I want to end up (output wise):
<!-- 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中访问_data(循环循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文