如何创建标签集合和类别集合
我在.eleventy.js
文件中有以下两个代码,
// Tags
eleventyConfig.addCollection('tagList', collection => {
const tagsSet = new Set();
collection.getAll().forEach(item => {
if (!item.data.tags) return;
item.data.tags.filter(tag => !['posts', 'all'].includes(tag)).forEach(tag => tagsSet.add(tag));
});
return Array.from(tagsSet).sort();
});
// Categories
eleventyConfig.addCollection('categoryList', collection => {
let catSet = new Set();
collection.getAll().forEach(item => {
if (!item.data.categories) return;
item.data.categories.filter(cat => !['posts', 'all'].includes(cat)).forEach(cat => catSet.add(cat));
});
return Array.from(catSet).sort();
});
我拥有的帖子
---
layout: post
title: 'Whatever'
date: '2021-10-12'
permalink: '{{ title | slug }}/index.html'
categories:
- code
tags:
- javascript
- angular
---
,我正在使用这样的模板来自动生成每个标签的页面(并且类别类似的模板)。
---
layout: page
eleventyComputed:
title: All posts tagged '{{ tag }}'
permalink: /tags/{{ tag }}/
pagination:
data: collections
size: 1
alias: tag
filter:
- all
- nav
- post
- posts
- tagList
addAllPagesToCollections: true
permalink: /tags/{{ tag }}/
headerButton:
url: '/blog/'
text: All posts
---
<ul class="unstyled">
{%- for post in collections[tag].reverse() -%}
{%- if not post.data.draft -%}
<li>{% include 'post-listing.njk' %}</li>
{%- endif -%}
{%- endfor -%}
</ul>
对于标签,一切正常。我得到了生成的正确页面IE /tags/javaScript
,但对于类别,其基于标签而不是类别生成页面。因此,代替/categories/code
我最终以/categories/javascript
我假设问题是我的everdentyconfig.addcollection('categoryList' )
功能,但我不知道为什么。
I have the following two blocks of code in my .eleventy.js
file
// Tags
eleventyConfig.addCollection('tagList', collection => {
const tagsSet = new Set();
collection.getAll().forEach(item => {
if (!item.data.tags) return;
item.data.tags.filter(tag => !['posts', 'all'].includes(tag)).forEach(tag => tagsSet.add(tag));
});
return Array.from(tagsSet).sort();
});
// Categories
eleventyConfig.addCollection('categoryList', collection => {
let catSet = new Set();
collection.getAll().forEach(item => {
if (!item.data.categories) return;
item.data.categories.filter(cat => !['posts', 'all'].includes(cat)).forEach(cat => catSet.add(cat));
});
return Array.from(catSet).sort();
});
In my posts I Have
---
layout: post
title: 'Whatever'
date: '2021-10-12'
permalink: '{{ title | slug }}/index.html'
categories:
- code
tags:
- javascript
- angular
---
And i'm using a template like this to auto generate pages for each Tag (and a similar one for categories).
---
layout: page
eleventyComputed:
title: All posts tagged '{{ tag }}'
permalink: /tags/{{ tag }}/
pagination:
data: collections
size: 1
alias: tag
filter:
- all
- nav
- post
- posts
- tagList
addAllPagesToCollections: true
permalink: /tags/{{ tag }}/
headerButton:
url: '/blog/'
text: All posts
---
<ul class="unstyled">
{%- for post in collections[tag].reverse() -%}
{%- if not post.data.draft -%}
<li>{% include 'post-listing.njk' %}</li>
{%- endif -%}
{%- endfor -%}
</ul>
For tags, everything is working fine. I get the correct pages generated i.e. /tags/javascript
but for categories, its generating pages based on the Tags and not the categories. So instead of /categories/code
I'm ending up with /categories/javascript
I'm assuming the issue is with my eleventyConfig.addCollection('categoryList')
function but I have no idea why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题可能是模板中的分页。您正在 collections object 对象映射标签(一个特殊的11ty Front Ittribute)与该标签的页面。
由于您正在创建标签和类别作为集合项目的列表,因此您需要为该集合分页。
这可能会构成问题,因为您正在模板中访问帖子,因此您需要一种将标签/类别映射回帖子列表的方法。您可以通过将收集更改为
对象
而不是array
来解决此问题。现在,当您要访问帖子时,您可以索引
collections.taglist
(或collections.categorylist
)。Your issue might be in the pagination in your template. You're paginating on the
collections
object, which is an object mapping tags (a special 11ty front matter attribute) to pages with that tag.Since you're creating a list of tags and categories as collection items, you'll want to paginate that collection instead.
This might pose an issue since you're accessing the posts themselves in the template, so you need a way to map the tag/category back to the list of posts. You can fix this by changing your collections to an
Object
instead of anArray
.Now, when you want to access the posts, you can index into
collections.tagList
(orcollections.categoryList
).您上面建议的一切都起作用。我现在有针对生成的标签和类别的单独页面,所以谢谢您。
我现在遇到的问题是,在我的博客页面上,我以前有一个类似输出的标签列表,因此
由于集合不再是数组,因此不再输出任何内容。
我更新到
标签上可以正常工作,但是它不适用于集合(使用
collections.CategoryList
),标签不再按字母顺序排列
任何建议?
更新:
输出类别的代码是
没有任何错误,但也没有输出。
如果我添加
| DIDSORT
对此,我得到错误模板渲染错误:dictsort filter:val必须是一个对象
Everything you suggested above has worked. I now have separate pages for tags and categories generated, so thank you for that.
The issue I now have is, on my Blog page, i previously had a list of Tags being output like so
This no longer outputs anything since the collection is no longer an array.
I updated to
and this works fine for Tags, but its not working for collections (using
collections.categoryList
)Also, the tags are no longer in alphabetical order
Any suggestions?
UPDATE:
The code for outputting categories is
This doesn't give any errors, but also no output.
If I add
| dictsort
to it, then I get the errorTemplate render error: dictsort filter: val must be an object