【问题标题】:Is it possible to have parent and child collections with Eleventy (11ty)?Eleventy(11ty)可以有父子集合吗?
【发布时间】:2020-03-23 06:27:22
【问题描述】:

我希望能够使用 Eleventy 来创建父集合和子集合,然后我可以循环创建导航。

我目前在一个名为 continents 的集合中有一些帖子,前面的内容显示如下:

---
title: Europe
tags: continents
---

我循环创建链接列表:

<ul class="parent-list">
  {% for post in collections.continents %}
    <li><a href="{{ post_url | post }}">{{ post.data.title}}</a></li>
  {% endfor %}
</ul>

是否可以拥有continents 的子集合?例如countries?如果是这样,这些数据需要添加到我的主题的什么位置?

如果能够像这样循环遍历集合,那就太好了:

<ul class="parent-list">
  {% for post in collections.continents %}
    <li><a href="{{ post_url | post }}">{{ post.data.title}}</a></li>
    <ul class="child-list">
      {% for post in collections.countries %}
        <li><a href="{{ post_url | post }}">{{ post.data.title}}</a></li>
      {% endfor %}
    </ul>
  {% endfor %}
</ul>

我知道eleventy-navigation,但看起来你也只能有一个级别的导航。

【问题讨论】:

  • eleventy-navigation 解决了这个确切的问题,它确实支持 N 级深度。具体来说,11ty.dev/docs 上的侧边栏显示 3 个不同的级别

标签: javascript html handlebars.js liquid eleventy


【解决方案1】:

据我所知,集合只有一层,所以你不能做collections.parent.child。您可以动态创建集合,例如拥有 collections.europe 和 collections.northamerica。然后你可以将你的第二个内部循环切换为这样的:

{% for post in collections.countries[post.data.title] %}

这有意义吗?

我应该补充一点,为了使它起作用,您的子帖子应该使用前端来设置他们的父级,例如:continent: europe

【讨论】:

  • 我还要补充一点——您也可以使用目录结构,例如 /posts/europe/france.md 和 /posts/europe/spain.md。在欧洲循环时,遍历 collections.all 并查看路径是否匹配。
猜你喜欢
  • 2021-11-14
  • 2021-09-18
  • 2011-09-05
  • 1970-01-01
  • 2021-05-10
  • 2021-10-19
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
相关资源
最近更新 更多