Use Jekyll Front Matter Defaults to set your layouts

I’ve been using Jekyll for years now and there are two things that I don’t like :

I thought I know Jekyll quite well but I discovered lately that my second issue have been solved for some time thanks to Front Matter Defaults. What’s this ?
It’s just a way to set some default values in the front matter of your content based on some rules either the path, the type, or the collection of a content.

Setting the default layout for all content

Until now, I had to set the layout everytime I create something and that was quite annoying because it’s always the my-default-layout layout that I use for a page and the my-post-layout when I create a post.

With Front Matter Defaults, it’s really easy to tell Jekyll what layout you want to use by default on all content you create. Add the following in your _config.yml to use the my-default-layout for every content :

defaults:
  -
    scope:
      path: "" # an empty string here means all files in the project
    values:
      layout: "my-default-layout"

Jekyll can’t reload itself when you edit your _config.yml, you’ll need to restart your Jekyll process everytime you edit it. Another thing, I obviously don’t name my layout like that, you’ll need to adjust the names of the layout to your installation.

After restarting and checking that everything works, you can remove all the layout lines from all your pages front matter, as it will be now set by default to my-default-layout.

Be careful if you want to use this if you have some CSS or JS files that have front matter, as Jekyll will add the default layout to those pages and won’t generate what you expect. In that case you will need to restrict the scope of the content you want to set the default values.

Setting the layout for all posts

Maybe like me you may not use the same layout for your pages and your content, in that case Front Matter Defaults can help you as well, you can add some value to only some type of content. I needed to apply the layout my-posts-layout to all my posts, it can be easily done with the following configuration :

defaults:
  -
    scope:
      type: "posts" # apply the value to all posts
    values:
      layout: "my-posts-layout"

Do not forget to restart Jekyll and as well remove every layout line from your front matter as it won’t be useful anymore…

Conclusion

With Front Matter Default, you’ll be able to ease your life a little more with Jekyll, as setting a layout for your content will not be required anymore. Now, all the pages and posts of my website have the simplest front matter possible :

---
title: Here is a title
description: And the description of my post
---

It’s a little thing but that’s a valuable change.