Loop thru frontmatter specified taxonomy, make a bullet list

Hello - I have a partial like this:

{{ "<!-- ENTERING layouts/partials/detailpage-topics.html -->" | safeHTML }}
{{ with .Params.showtopics }}
<ul>
{{ range $mytopic := . }}
{{ $mytopiclc := lower $mytopic }}
{{ $mytopicpages := index $.Site.Taxonomies.topics $mytopiclc }}
{{ range $mytopicpages.Pages }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}
{{ end }}
</ul>
{{ end }}
{{ "<!-- LEAVING layouts/partials/detailpage-topics.html -->" | safeHTML }}

It is called in my single.html and looks at a markdown page’s frontmatter for:

showtopics:
  - this
  - that

…, loops through those, and uses the string to find pages labeled with a given topic, in the taxonomy topics.

I tried many variations of the code, finding out that I have to use lowercase for the comparison, and tried combining so that the lower function is used directly.

My question is, is there a more efficient way to do this, or, does this look basically correct? I was thinking that it might be nice to have this in a shortcode, instead, since then I could specify where the list appears. Currently, it’s just fixed in single.html after the {{ .Content }}.

If I understood you correctly, you want to reuse this code.

You can do that by:

{{partial "detailpage-topics.html" .}}

And no need to fret too much about efficiency. This is hugo. Even if you write outright glaringly inefficient templates, it would still hardly be noticeable during compile time. :slight_smile:

Hi @ashishthedev. Yes, I’m calling it that way already. Just wondering if there is a better way to code it. I’d rather use the best way.

Looks simple and fine to me.

1 Like

Thanks @bep