Eleventy Recipes Wordmark

Eleventy Recipes

Add front matter to all files in a directory

By Mike Aparicio

Rather than add the same front matter to every file in a directory, Eleventy will automatically apply any values in a JSON file with the same name to every file.

Prerequisites

Add a layout template

Directions

  1. Create a folder called posts in the root of your project
  2. Create a file called posts.json in the posts folder and add the following:
{
"layout": "base"
}
  1. Create another file in posts called my-first-post.md. You only need to add front matter for the title.
---
title: My First Post
---
  1. Create a second post, my-second-post.md. Again, you only need to add a title.
---
title: My Second Post
---
  1. You can now visit either of these pages, http://localhost:8080/posts/my-first-post/ and you'll see the page is using our base layout without having needed to specify it in each page's front matter.

Resources