Table of Contents
Hugo is a popular static site generator that allows content creators to build fast and flexible websites. One of its powerful features is archetypes, which help maintain consistency across your content. By using archetypes, you can ensure that each new post or page follows a predefined structure, saving time and reducing errors.
What Are Archetypes in Hugo?
Archetypes in Hugo are template files that define default front matter and content for different types of content. When you create a new post or page using the command line, Hugo automatically applies the relevant archetype, populating fields like title, date, categories, and custom parameters.
Creating and Customizing Archetypes
To create an archetype, you need to add a file in the archetypes directory of your Hugo project. For example, to create an archetype for blog posts, create a file named post.md with your desired default front matter:
---
title: "My New Post"
date: {{ .Date }}
categories: ["blog"]
tags: ["hugo", "archetypes"]
draft: true
---
You can customize this template with any front matter parameters you want to standardize across your posts. When creating a new post, Hugo will use this archetype as a starting point.
Using Archetypes When Creating Content
To create new content with an archetype, use the Hugo CLI command. For example, to create a new post using the post archetype, run:
hugo new post/my-first-post.md
This command generates a new Markdown file in the content/post directory, pre-filled with the front matter defined in archetypes/post.md. You can then customize the content as needed.
Benefits of Using Archetypes
- Consistency: Ensures all content follows a uniform structure.
- Efficiency: Saves time by auto-filling common fields.
- Accuracy: Reduces errors and omissions in front matter.
- Flexibility: Easily customize archetypes for different content types.
By leveraging Hugo’s archetypes, content creators can streamline their workflow, maintain quality standards, and focus more on writing rather than formatting. Proper use of archetypes is a key step towards building a well-organized and professional website.