Hugo - Archetypes

As you grow your meta tags in the previous chapter, you'll notice there are a lot of information needed for a content to display properly to the robots. These information are meant for crawler robots such as Google Spider to understand your page without running through the page. Yeap, it is related to the basic of Search Engine Optimization.

However, if you notice your newly generated Markdown, it only generate a very simple YAML based Front Matters:

---
title: "License"
date: 2018-08-08T10:36:12+08:00
draft: true
---

But you might want the newly generated Markdown to create this TOML Front Matters instead:

<!--
+++
title = "Title"
date = 2018-08-08T10:36:12+08:00
draft = true
description = "post description is here. It will be shown in Google Search bar"
keywords = "post, keyword1, keyword2"
+++
-->

# Your Main Title Here
....

Enter the archetypes. To facilitate the custom "new" template, we create them inside the archetypes directory, as per specification. In this checkpoint, we'll be creating one for familiarization.

Objectives

  1. To create archetypes for our hugo directory.
Hugo - Archetypes - Create Default

Create the Default Archetypes

From the specification, we learn that archetypes always refer to default.md in its folder as a last resort. Let's create one by using the example above. Save it as:

archetypes/default.md


Hugo - Create Archetypes - Verify

Create New Content To Verify

Now that we have the new template, let's create a dummy page to verify it.

$ hugo new posts/dummy.md

Open it up and see the result. If the front matters changed into the version we provide, Congratulations; you had done it!

Hugo - Archetypes search path

Section Specific Archetypes

Based on the specification, we can create section specific archetypes by creating the folder matching the content folder name. Example,

  1. archetypes/posts.md is for content/posts/
  2. archetypes/docs.md is for content/docs/


This way, we can create various versions of archetypes for different sections in contents.

Summary

Now that we implemented the archetypes, we can conclude the checkpoint. Remember, you can always change the "new" templates by using archetypes.