Using Archetypes in Hugo

Published on 01/7/2020 | 7.01.12020 HE

For the longest time I completely ignored archetypes in hugo. I’d always copy over the front-matter from the last blog post I wrote and then fill in the things I needed - leading to some mistakes (e.g. using category instead of categories) over time.

Archetypes are easy to use, for example to create this post I ran the following command

$ hugo new blog/

This copied over the archetype file from archetypes/ and automatically filled in the post tile, date, last modified date, and the defaults for categories, tags, type, and whatever else I have in the template file. Since Hugo 0.49 archetypes can consist of an entire directory structure that will be created. This is helpful for my photography section where a post consists of an file and some images that are automatically loaded. The structure looks like this.

└── photography

For this type of post the --kind flag must be added to the command as shown below.

$ hugo new photography/my-photo-post

This command will create a directory named my-photo-post in content/photography/my-photo-post. Neat!

Hugo template magic

As if this wasn’t good enough, we can use the full set of Hugo template functions and script even more!

The archetype for new blog posts I use looks like this.

title: "{{ replace (replace .Name "-" " ") (now.Format "2006 01 02 ") "" | title }}"
intro_text: ""
type: blog
    - coding
date: "{{ now.Format "2006-01-02" }}"
lastmod: "{{ now.Format "2006-01-02" }}"

In the first line the post title is set which is taken from the file name. I use filenames like on my blog which include the full date and then the post tile and the .md file extension for markdown. The .md is automatically stripped away which leaves a string like 2020-01-02-a-post. The first replace (replace .Name "-" " ") turns this into 2020 01 02 a post, the next replace (replace (replace .Name "-" " ") (now.Format "2006 01 02 ") "") takes this string and strips the date, see the detailed steps below.

We assume that .Name equals “2020-01-02-a-post”:

  1. replace (replace .Name "-" " ") (now.Format "2006 01 02 ") ""
  2. replace "2020 01 02 a post" (now.Format "2006 01 02 ") ""
  3. replace "2020 01 02 a post" "2020 01 02" ""
  4. "a post"

So after the whole code is executed, we have the plain title. This title is then passed to the function named title which is doing capitalization, leaving us with a nicely formatted title.

As shown above, I also set the date and lastmod times using Hugo template functions.

date: "{{ now.Format "2006-01-02" }}"
lastmod: "{{ now.Format "2006-01-02" }}"

Both dates will be set to the same as this (new) article hasn’t been modified yet. lastmod does only change when I update an article later on.



Like this?

This website contains no advertisement, no tracking, no paid articles, and no shady-things-whatsoever. It is just content, provided free of charge and with accessibility in mind.

If you would like to support me in providing content - and code - for free or if this page has helped you or your company, consider supporting me directly.

You can support me via:

A colorful image of me wearing my DIY mask. The image has a colorful glitch effect.

Kevin Gimbel

is a DevOps Engineer and avid (Video) Gamer. He’s also interested in Sci-Fi, Cyberpunk, and dystopian books.

Wearing a mask is a good idea!

You can find out more about me if you wish.