← 11ty notes

Applying frontmatter defaults to a whole folder of posts in eleventy

Using JSON data files to apply defaults

• 🍿 1 min. read

Why do this?

Eleventy uses YAML front matter at the top of your mardown files to specify variables and templates to use.

Example:

---
layout: layouts/tutorial.html
title: My first tutorial
---

However if you have a whole folder of markdown posts, specifying things that are the same across all posts (like the layout) is uneccessarily repetitive, and painful if you want to change things later.

Solution

Skip the common variables (like layout) and add them into a JSON file in the root of your directory of posts.

/src/tutorials/tutorials.json

{
"layout": "layouts/tutorial.html"
}

Make sure it's named the same as your directory.

Then you can skip having to specify layout in your markdown files:

---
title: My first tutorial
---

...which simplifies things you have lots of posts, and means you have a single place to make any future changes. Find out more about Template and Directory specific data files in the 11ty docs.


Check out other things I've written tagged: eleventy

← 11ty notes