Changing my Series to Taxonomy

August 27, 2017
4 min. read

This post is part of the Hugo series.

When trying to implement a series sidebar and stand alone page in Hugo, I saw advantages of using the taxonomies structure in hugo.

config.toml

To implement series as a taxonomy, I added it to my config.toml. That section now contains three taxonomies.

[taxonomies]
    category = "categories"
    series = "series"
    tags = "tags"

Front Matter

My only change to front matter from my previous series implementation was to change into an array style. Instead of series = "Hugo" I need to use series = ["Hugo"]. Taxonomies allow multiple membership, but I will only ever be using a single on with series.

Series Widget

I wanted to add various series to my sidebar. It was looking at categories display I already had that made this obvious why I should convert into taxonomies with series. In my theme, I created a new partials/widgets/series.html Below is the contents of this file. This gives the series name and the count of posts for each series. The results of this is visible in the sidebar.

{{ if isset .Site.Taxonomies "series" }}
{{ if not (eq (len .Site.Taxonomies.series) 0) }}
<div class="panel panel-default sidebar-menu">

    <div class="panel-heading">
      <h3 class="panel-title">Blog Series</h3>
    </div>

    <div class="panel-body">
        <ul class="nav nav-pills nav-stacked">
            {{ range $name, $items := .Site.Taxonomies.series }}
            <li><a href="{{ $.Site.BaseURL }}series/{{ $name | urlize | lower }}" class="text-uppercase">{{ $name }} ({{ len $items }})</a>
            </li>
            {{  end }}
        </ul>
    </div>
</div>
{{ end }}
{{ end }}  

I created a template in partials to put a note at the top of the post, if this is part of a series. This is pretty simple (and you can see the results at the top of this post.) This partial is included in the page rendering just above {{ .Content }}.

{{if .Params.series}}
  {{ $name := index .Params.series 0 }}
  <p class="series_top_page">This post is part of the <a href="{{.Site.BaseURL}}/series/{{$name | urlize}}">{{$name}}</a> series.</p>
{{end}}

By having this as a taxonomy, I did not have to generate the series page.

This is the only functionality that existed previously. It was also the most difficult for figure out.

To understand this template, look in my previous series post in the Hugh series. I’ll just be discussing the changes here. {{ $series_name := index .Params.series 0 }} pulls the first (and only as we only have one) series from the array and stores in $series_name.

I could not get the range where to function with the array style of series front matter, rather than just a value. I wound up having to loop all pages, then using an if to filter. The same basic functionality was done on the reversed one.

{{ range .Site.RegularPages.ByDate }}
    {{ if in .Params.series $series_name }}

Other than those, the functionality is exactly the same. Below is the full template to make the bottom links as you see on this page.

{{- if .Params.series -}}
    {{- $series_name := index .Params.series 0 -}}
    {{- $.Scratch.Add "cur_page_num" 1 -}}
    {{- $.Scratch.Add "total_page_num" 0 -}}
    {{- range .Site.RegularPages.ByDate -}}
        {{- if in .Params.series $series_name -}}
            {{- $.Scratch.Add "total_page_num" 1 -}}
            {{- if gt $.Date.Unix .Date.Unix -}}
                {{- $.Scratch.Add "cur_page_num" 1 -}}
                {{- $.Scratch.Set "prev_link" .Permalink -}}
                {{- $.Scratch.Set "prev_title" .Title -}}
            {{- end -}}
        {{- end -}}
    {{- end -}}

    {{- range .Site.RegularPages.ByDate.Reverse -}}
        {{- if in .Params.series $series_name -}}
            {{- $.Scratch.Set "first_link" .Permalink -}}
            {{- if lt $.Date.Unix .Date.Unix -}}
                {{- $.Scratch.Set "next_link" .Permalink -}}
                {{- $.Scratch.Set "next_title" .Title -}}
            {{- end -}}
        {{- end -}}
    {{- end -}}
    {{- if or ($.Scratch.Get "next_link") ($.Scratch.Get "prev_link") -}}
        <hr/>
        <p>Part {{ $.Scratch.Get "cur_page_num" }} of {{ $.Scratch.Get "total_page_num" }}
            in the <b>{{- $series_name -}}</b> series.</p>
        <p>
        {{- if $.Scratch.Get "prev_link" -}}
            {{- if ne ($.Scratch.Get "prev_link") ($.Scratch.Get "first_link") -}}
                <a href="{{- $.Scratch.Get "first_link" -}}"><i class="fa fa-angle-left"></i>Series Start<i class="fa fa-angle-right"></i></a>
                     |
            {{ end -}}
            {{- if $.Scratch.Get "prev_link" -}}
                <a href="{{- $.Scratch.Get "prev_link" -}}"><i class="fa fa-angle-double-left"></i>{{- $.Scratch.Get "prev_title" -}}</a>
            {{- end -}}
        {{- end -}}
        {{- if and ($.Scratch.Get "next_link") ($.Scratch.Get "prev_link") }}
                 |
        {{ end -}}
        {{- if $.Scratch.Get "next_link" -}}
            <a href="{{- $.Scratch.Get "next_link" -}}">{{- $.Scratch.Get "next_title" -}}<i class="fa fa-angle-double-right"></i></a></p>
        {{- end -}}
    {{- end -}}
{{- end -}}

At first, I wasn’t using the {{- and -}} where I could. With all this looping, I wound up with 1200+ blank lines in my html! View source to see what templates can be cleaned up.

If something on this doesn’t make sense, let me know and I’ll try to make it clearer.


Part 4 of 4 in the Hugo series.

Series Start | Disabling Analytics for Local Hugo

comments powered by Disqus