11ty default front matters

Default Front Matter in 11ty

If you're using Eleventy (11ty) for your static site generation, you're probably familiar with front matter - the metadata at the top of your Markdown or template files. But did you know that 11ty provides powerful ways to set default front matter?

This feature can significantly streamline your workflow and maintain consistency across your site.

Let's dive in!

What is Default Front Matter?

Default front matter allows you to specify metadata that applies to multiple files or entire directories without repeating yourself in each file.

This is especially useful for properties like layouts, tags, or categories that are often shared across related content.

Three Ways to Set Default Front Matter in 11ty

1. Directory Data Files

For directory-specific defaults, create a JSON file named after the directory. For example, posts/posts.json:

{
  "layout": "post.njk",
  "tags": ["post"]
}

This applies to all files in the posts directory and its subdirectories.

2. Template Data Files

For defaults specific to a template type, use a file like markdown.json:

{
  "layout": "base.njk"
}

This applies to all Markdown (.md) files in your project.

3. Global Data Files

For project-wide defaults, use the _data directory. Create a file like _data/metadata.js:

module.exports = {
  title: "My Awesome Site",
  url: "https://example.com",
  author: {
    name: "Your Name",
    email: "you@example.com"
  }
};

This data is available to all templates.

Overriding Default Front Matter

You can always override default front matter in individual files:

---
layout: special-post.njk
title: My Unique Post
---

Content here...

Benefits of Using Default Front Matter

  1. Reduced Repetition: Write less boilerplate in each content file.
  2. Consistency: Ensure shared properties are uniform across related content.
  3. Easier Maintenance: Update shared properties in one place.
  4. Cleaner Content Files: Focus on content, not metadata.

Best Practices

  1. Use directory data files for section-specific defaults.
  2. Use global data for site-wide information.
  3. Keep your default front matter DRY (Don't Repeat Yourself).
  4. Document your default front matter structure for team collaboration.

By mastering default front matter in 11ty, you'll create a more efficient and maintainable static site workflow.