Search code examples
eleventy

11ty render single post per data file


I want 11ty to generate a single page per data file, rather than generate multiple files by iterating over the data within a data file. Is this possible?

My use case is that I have multiple job descriptions in yaml format: One yaml page per job. So I want to generate a page for each data file.

I realise I could put all the yaml data as front matter in a page with no content, but this feel wrong as I only have the data.

Thanks for any pointers.


Solution

  • Yes, you can use 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.

    The example in the linked documentation article uses one data file (possums.json) and paginates over the object that eleventy generates for that file. If you have one data file per element (job, in your case), you can achieve the same thing by putting all those datafiles inside a parent directory inside the _data directory, and iterate over that. The key here is understanding how eleventy handles nested directories in the global data directory and that you can use pagination with an object.

    So if you have your yaml job files structured like this:

    • _data/jobs/job1.yaml
    • _data/jobs/job2.yaml
    • _data/jobs/job3.yaml

    You should be able to paginate them like this (adapted from the example in the documentation, might have to adjust a bit to make it work with your content):

    pagination:
        data: jobs
        size: 1
        alias: job
        resolve: values
    permalink: "jobs/{{ job.name | slug }}/"