十一:创造"View all posts in collection"link

Eleventy: Create "View all posts in collection" link

我正在尝试在 Eleventy 中建立一个站点。该站点在两个集合中有博客 post:A 和 B。当我阅读集合 A 中的 post 时,我希望在 [= 之后有一个 link 22=] 内容表示“查看所有 A posts”。当我阅读集合 B 中的 post 时,我希望看到“查看所有 B posts”link.

博客 post 共享相同的 _includes 模板 post.html。在该模板中,我应该能够在 nunjucks 中创建一个如下所示的 if 语句:

{% if post in collections.A %}<a href="/a">View all A posts</a>
{% elif post in collections.B %}<a href="/b">View all B posts</a>
{% endif %}

虽然该代码片段不起作用。有什么想法吗?

鉴于您的博客 post 由文件夹分隔,您可以创建一个目录数据文件,为目录 (A, B) 指定一个值。像这样:

{
"blogtype":"a"
}

在您要执行 link 的 post 模板中,您可以检查 blogtype 值来执行 link。

这有意义吗?明确地说,您可以使用过滤器执行“如果此 post 在某某集合中”的逻辑,但我认为指定目录数据文件(可能)会更简单。

我在那里得到答案https://www.webstoemp.com/blog/multilingual-sites-eleventy/

module.exports = function (eleventyConfig) {
  eleventyConfig.addCollection("posts_en", function (collection) {
    return collection.getFilteredByGlob("./src/en/posts/*.md");
  });
};

module.exports = function (eleventyConfig) {
  eleventyConfig.addCollection("posts_fr", function (collection) {
    return collection.getFilteredByGlob("./src/fr/posts/*.md");
  });
};