Mikke.Learns

Adding tags to posts in a HUGO template using the Params variable

January 26th, 2019

Reading the HUGO docs is challenging, especially when I’ve got no experience with HUGO or GO. After learning Javascript, I have an understanding of what the Hugo/Golang code is doing, but I don’t know how to write it and I don’t understand all the syntax.

So when I wanted to add tags to my list of post, I went through the docs and found an answer under the Page Variables section.

The way I understand it is that all values defined in the front matter in content files, including taxonomies, are accessible through the Params variable.

In order to access tags and categories, we can use Params.tags and Params.categories.

  • {{ .Params.tags}}

  • {{ .Params.categories}}

I tried to implement this, but it failed miserably. I had to find a template and copy the following code to make it work:

{{ range .Params.tags }} {{ $href := print (absURL "tags/") (urlize .) }} <small><code><a href="{{ $href }}">{{ . }}</a></code></small> {{ end }}

With the Params variable, we can access all custom front matter fields, even those who are nested. --- movie: title: Jurassic Park director: Steven Spielberg year: 1993 --- By concatenating the field names with a dot (.), we are able to display the chosen value: {{ $.Param "movie.title" }}