Hugo Site Fix

created: 2022/11/01

Summary

Some details about the fix I just made to my hugo list template. I had some customized list templates which broke recently and this covers the problem and fix.

The Break-age

My list templates were previously using a (somewhat hackish?) method of listing all pages. I can not remember now why I did that or where I found the original snippet. It looked something like this.

This is an example of broken code it does NOT work

{{- $currentSection := .CurrentSection -}}
{{- $all_pages := where .Site.RegularPages "Section" .Section -}}
{{- range $all_pages -}}
  {{- if in (.Permalink | string) $currentSection.RelPermalink -}}
    {{- $pages = $pages | append . -}}
  {{- end -}}                                                                                   
{{- end -}}

In a nut-shell the code is basically creating a slice to hold a subset of pages in $pages. Then it grabs the current section, and gets a list of all .RegularPages for the current .Section. The hack-ish part is that it then iterates $all_pages to match the .Permalink for the page against the $currentSection.RelPermalink. I seem to remember doing this because of needing to filter tags specially because of some of my url paths? I could be totally wrong though.

In any event that stopped working for some reason.

The Fix

The fix is pretty simple and a lot cleaner. Essentially we use Params.tags and intersect that with a slice containing a single tag which we get from .Title.

{{- $pages := slice -}}
{{- $pages := where .Site.RegularPages "Params.tags" "intersect" (slice .Title) -}}

I have a few other custom checks in my list.html template to include the additional tag and post links. Something like the following.

{{ if or (.Parent.IsSection) (eq .Parent.Kind "taxonomy") }}
<li><a href="{{.Parent.RelPermalink}}">All {{ .Parent.Title }}</a></li>
<li><a href="/posts">All Posts</a></li>
{{- else -}}
  {{ range .Sections }}
  <li><a href="{{.RelPermalink}}">All {{ .Title }} Posts</a></li>
  {{ end }}
{{ end }}

Hopefully this helps someone get their own taxonomy working!

The Meta

0.1

2022/11/01
  • Initial Post

0.1

2022/10/31
  • Started Writing