Search code examples
eleventy

How do you specify a default layout for all pages in eleventy?


It seems like you should be able to specify a fallback layout for all pages in an 11ty site using global data files and the data cascade, but I can't figure out how.

I've tried the following JSON in several locations:

{
  "layout": "layouts/page.njk"
}

I've put this JSON in:

  • _data/default.json
  • _data/site.json
  • _data/site.11tydata.json
  • _data/11tydata.json

So far no luck.


Solution

  • This is actually something which is (as far as I'm aware) not yet possible by default in Eleventy. You can use a directory data file to specify a layout for all files nested within that directory, but that doesn't currently work from the root of the project. There was a feature request for this on GitHub.

    In that same GitHub issue a workaround was suggested, which works pretty well: namely to make a layout.js file in the _data folder which exports a string pointing to the layout location.

    I gave it a quick test just now with the following setup, and it seems to work as desired:

    enter image description here

    I hope that helps!