每个数据文件 11ty 渲染单个 post
11ty render single post per data file
我希望 11ty 为每个数据文件生成一页,而不是通过遍历数据文件中的数据来生成多个文件。这可能吗?
我的用例是我有多个 yaml
格式的职位描述:每个职位一个 yaml 页面。所以我想为每个数据文件生成一个页面。
我意识到我可以将所有 yaml 数据作为前端内容放在一个没有内容的页面中,但感觉不对,因为我只有数据。
感谢您的指点。
是的,您可以使用 collections combined with pagination to create one output file per job. The key is using a pagination size of 1, this way you only need one page in your site directory, and it will create one output page per job. This is documented in Create Pages From Data。
链接文档文章中的示例使用一个数据文件 (possums.json
) 并对 eleventy 为该文件生成的对象进行分页。如果每个元素(在您的情况下为工作)有一个数据文件,则可以通过将所有这些数据文件放在 _data 目录内的父目录中并对其进行迭代来实现相同的目的。这里的关键是理解 eleventy 如何处理 nested directories in the global data directory and that you can use pagination with an object.
因此,如果您的 yaml 作业文件结构如下:
_data/jobs/job1.yaml
_data/jobs/job2.yaml
_data/jobs/job3.yaml
您应该能够像这样对它们进行分页(改编自文档中的示例,可能需要稍作调整以使其与您的内容兼容):
pagination:
data: jobs
size: 1
alias: job
resolve: values
permalink: "jobs/{{ job.name | slug }}/"
感觉刚刚好 :) 将文件放入其中,几秒钟内即可完成您的页面。如果您只需要将它们转换为页面,这是最快的方法。
Collections会给你listing等等
我希望 11ty 为每个数据文件生成一页,而不是通过遍历数据文件中的数据来生成多个文件。这可能吗?
我的用例是我有多个 yaml
格式的职位描述:每个职位一个 yaml 页面。所以我想为每个数据文件生成一个页面。
我意识到我可以将所有 yaml 数据作为前端内容放在一个没有内容的页面中,但感觉不对,因为我只有数据。
感谢您的指点。
是的,您可以使用 collections combined with pagination to create one output file per job. The key is using a pagination size of 1, this way you only need one page in your site directory, and it will create one output page per job. This is documented in Create Pages From Data。
链接文档文章中的示例使用一个数据文件 (possums.json
) 并对 eleventy 为该文件生成的对象进行分页。如果每个元素(在您的情况下为工作)有一个数据文件,则可以通过将所有这些数据文件放在 _data 目录内的父目录中并对其进行迭代来实现相同的目的。这里的关键是理解 eleventy 如何处理 nested directories in the global data directory and that you can use pagination with an object.
因此,如果您的 yaml 作业文件结构如下:
_data/jobs/job1.yaml
_data/jobs/job2.yaml
_data/jobs/job3.yaml
您应该能够像这样对它们进行分页(改编自文档中的示例,可能需要稍作调整以使其与您的内容兼容):
pagination:
data: jobs
size: 1
alias: job
resolve: values
permalink: "jobs/{{ job.name | slug }}/"
感觉刚刚好 :) 将文件放入其中,几秒钟内即可完成您的页面。如果您只需要将它们转换为页面,这是最快的方法。 Collections会给你listing等等