Hugo Section Tags
Overview
For sometime, I had the need to trim down the taxonomies according to the content of sections. I could not find any valid and reusable solution until I figured out a possibility. Note : this method will not work for everyone. It does not generate different Tags pages per section, but only limit the list of tags per section.
My solution
Reading the doc on Hugo’s website, I found out this Method : .RegularPagesRecursive
which allows me to get all the pages from the current section.
I m startting to create a slice, then I m iterating the section pages through .RegularPagesRecursive
, I m checking the tags for each pages, and I m adding them in the slice if they are not yet present.
Once I m done, I m creating my list of tags by only showing the tags present in my slice. This means when I m clicking on the link, the page will show all the links of that pages associated to the tag even if they were not present in the section, but this is not an issue in my case.
{{ $localtags := slice }}
{{ $sectionPages := .RegularPagesRecursive }}
{{ range .RegularPagesRecursive }}
{{ range $pageSystem := .Params.tags}}
{{ $pageSystem = upper $pageSystem }}
{{ if in $localtags $pageSystem }}
{{else}}
{{ $localtags = $localtags | append $pageSystem }}
{{end}}
{{end}}
{{ end }}
<div><strong>Tags</strong></div>
<ul class="list-group">
{{ range .Site.Taxonomies.tags }}
{{ $current := upper .Page.Title}}
{{ if in $localtags $current }}
<li class="list-group-item"><a class="btn btn-default text-start" role="button"
href="{{ .Page.Permalink }}">{{ .Page.Title }} <strong>{{ .Count }}</strong></a></li>
{{end}}
{{ end }}
</ul>