What Are Hugo Archetypes?

Dec 17, 2019 Programming, Hugo,

archetype (n.)
model, first form, original pattern from which copies are made...

To be concise, Hugo archetypes are templates for generating new content like posts and pages. You may or may not be surprised to learn that if you’ve ever used the command hugo new then you’ve already been using archetypes.

When you create a new site, Hugo will create a default archetype for you in the archetypes folder called default.md. It will look something like this.

1---
2title: "{{ replace .Name "-" " " | title }}"
3date: {{ .Date }}
4draft: true
5---

And when you run the command hugo new posts/my-post.md Hugo will:

  1. search for an archetype based on the file path. Because you specified posts as the content directory, Hugo will look for archetypes/posts.md. If Hugo can’t find that archetype it will use the default archetype.

    If you had asked Hugo to create a page (e.g. hugo new pages/about.md) it would have looked for an archetype called archetypes/pages.md.

  2. replace the variables in that archetype. In the example above, Hugo would replace the variables .Name and .Date.

  3. create a new file in the content directory called content/posts/my-post.md.

Archetypes are useful because they allows us to automate the tedious process of creating files in the content directory and adding metadata like title, date and draft. Archetypes are also highly customizable. They aren’t just limited to posts and pages. You can create archetypes for any type of content that you want. You can even use a directory as an archetype.

Thanks to MarĂ­n Alcaraz for editing.
2019-2020, Raymond Sohn.