Prev/Next Taxonomy term in Taxonomy lists

I’m writing a template for Taxonomy list (one that is used for rendering /PLURAL/TERM). In that template, I’d like to get previous and next term within single Taxonomy. Is there a sane way to do this?

I’ve ended up with this code:

{{ $taxonomy_name := "hg" }}
{{ $term_pages := index .Data $taxonomy_name }}
{{ $firstpage := index $term_pages 0 }}
{{ $term_name := index (index $firstpage.Page.Params $taxonomy_name) 0 }}

    {{ $all_terms := index .Site.Data $taxonomy_name }}
    {{ $.Scratch.Set "previously_seen" "" }}
    {{ range $key, $value := $all_terms }}
      {{ $prev := $.Scratch.Get "previously_seen" }}
      {{ if eq $key $current_term_name}}Previous term: {{ $prev }}<br>{{ end }}
      {{ if eq $prev $current_term_name }}Next term: {{ $key }}<br>{{ end }}
      {{ $.Scratch.Set "previously_seen" $key }}
    {{ end }}

This works, but is ugly. I’ve tried using a normal variable instead of Scratch, but it looks like the scope of variable set inside a range loop is limited to single iteration.

Does anyone see a saner way of doing this?

I did it like this:

{{ $current_database_key := .Params.database_key }}
{{ $current_page := . }}

{{ with .Site.GetPage "taxonomyTerm" "tutorial_types" }}
  {{ range .Data.Pages.ByWeight }}
    {{ range sort .Pages .Params.tutorial_types_weight "asc" }}
      {{ if in .Params.database_key $current_database_key }}
        {{ if eq . $current_page }}
          {{ $.Scratch.Set "previous_page" ($.Scratch.Get "previous_pointer") }}
        {{ end }}

        {{ if eq ($.Scratch.Get "previous_pointer") $current_page }}
          {{ $.Scratch.Set "next_page" . }}
        {{ end }}

        {{ $.Scratch.Set "previous_pointer" . }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

<div class="text-small mt-2">
  {{ if $.Scratch.Get "previous_page" }}
    <a href="{{ ($.Scratch.Get "previous_page").RelPermalink }}">
      <i class="left chevron icon"></i>
      {{ ($.Scratch.Get "previous_page").Title }}
    </a>
  {{ end }}

  {{ if $.Scratch.Get "next_page" }}
    <a style="float: right;" href="{{ ($.Scratch.Get "next_page").RelPermalink }}">
      {{ ($.Scratch.Get "next_page").Title }}
      <i class="right chevron icon"></i>
    </a>
  {{ end }}
</div>

Now $.Scratch.Get "previous_page" and $.Scratch.Get "next_page" will contain the pages you want.

I know this post is old, but I kept coming across it when searching for examples while rebuilding my portfolio in Hugo. Here’s a solution I came up with that’s worked for me in case it helps someone else.

In my site config I use this to define taxonomies and set their path beneath a /portfolio area on my site:

[taxonomies]
  client = "clients"
  market = "markets"

[permalinks]
  clients = "/portfolio/clients/:title/"
  markets = "/portfolio/markets/:title/"

Then for pages such as /portfolio/clients/acme or /portfolio/markets/b2b, the template at layouts/term/list.html is used and I include this as a partial:

{{ $taxonomy := .Section }}
{{ $terms := (where .Site.Pages "Type" "in" $taxonomy).ByTitle }}
{{ $firstTerm := index $terms 0 }}
{{ $lastTerm := index $terms (sub (len $terms) 1) }}
<!-- initialize prev/next vars with a default value in page scope -->
{{ $prevTerm := . }}
{{ $nextTerm := . }}

<!-- test if a prev/next term currently exists and set, -->
<!-- else loop through list by setting to show last/first term -->
{{ with $terms.Prev . }}
{{ $nextTerm = . }}
{{ else }}
{{ $nextTerm = $firstTerm }}
{{ end }}

{{ with $terms.Next . }}
{{ $prevTerm = . }}
{{ else }}
{{ $prevTerm = $lastTerm }}
{{ end }}

<section role="navigation" aria-label="Previous/Next {{ title (singularize $taxonomy) }}" class='container prev-next'>
    <a href="{{- $prevTerm.RelPermalink -}}" class="previous-term" rel="prev">
        <span class="section-label"><span class="order-label">Prev </span>{{- title (singularize $taxonomy) -}}</span>
        <span class="truncate">{{- $prevTerm.Title -}}</span>
    </a>
    <a href="{{- $nextTerm.RelPermalink -}}" class="next-term" rel="next">
        <span class="section-label"><span class="order-label">Next </span>{{- title (singularize $taxonomy) -}}</span>
        <span class="truncate">{{- $nextTerm.Title -}}</span>
    </a>
</section>