Efficient way to list all taxonomies of a page

Hi!

I want to add a breadcrumb to my template that reflects the taxonomies for the current page. This is easy if you know the taxonomies upfront. But if you want to have this in a generic template that does not know the name of the taxonomies, the only way I can think of is walking through all taxonomies (.Site.Taxonomies) and match them with the params of the page (.Params). The template becomes ugly and this can’t be efficient.

Is there any better way to do this?

Best regards
Tobias

Look, I know this thread is like 6 years old, but it is also (at time of writing) the first DuckDuckGo result for “hugo template list all taxonomies on page” and it has no answers. So while I haven’t found a very clean and simple solution, I figured I could at least share a solution:

<aside>
{{- range $siteTaxonomyName, $siteTaxonomyData := site.Taxonomies }}
    {{- with $.Page.GetTerms $siteTaxonomyName }}
    <h4>{{ $siteTaxonomyName | title }}:</h4>
    <ul>
    {{- range . }}
        <li><a href="{{ .RelPermalink }}">{{ .Title | title }}</a></li>
    {{- end }}
    </ul>
    {{- end }}
{{- end }}
</aside>

This is probably very similar to what you’ve already figured out. The above snippet loops through the site’s taxonomies, and if the page has terms from that taxonomy, it prints that taxonomy name, and then the page’s terms for that taxonomy in a list.

One notable shortcoming is that site.Taxonomies seems to always return the list of taxonomies in alphabetical order. If instead I always want the tags taxonomy to appear first, I have to take a few more steps:

{{- $preferredTaxonomies := slice "tags" "categories" }}

<!-- Get list of taxonomies on this page -->
{{- $taxonomiesInPage := slice }}
{{- range $siteTaxonomyName, $siteTaxonomyData := site.Taxonomies }}
    {{- with $.Page.GetTerms $siteTaxonomyName }}
        {{- $taxonomiesInPage = $taxonomiesInPage | append $siteTaxonomyName }}
    {{- end }}
{{- end }}

<!-- Reorder taxonomy list so that taxonomies in $preferredTaxonomies show up first -->
{{- if gt ($taxonomiesInPage | len) 0 }}
    {{- range $preferredTaxonomy := $preferredTaxonomies | collections.Reverse }}
        <!-- If page has a preferred taxonomy, place taxonomy at the start of the list -->
        <!-- Duplicates are covered by uniq -->
        {{- if in $taxonomiesInPage $preferredTaxonomy }}
            {{- $taxonomiesInPage = append $taxonomiesInPage (slice $preferredTaxonomy) }}
        {{- end }}
    {{- end }}
    {{- $taxonomiesInPage = uniq $taxonomiesInPage}}
{{- end }}

<!-- render taxonomy lists -->
<aside>
{{- range $taxonomyName := $taxonomiesInPage }}
{{- with $.Page.GetTerms $taxonomyName }}
    <h4>{{ $taxonomyName | title }}:</h4>
    <ul>
    {{- range . }}
        <li><a href="{{ .RelPermalink }}">{{ .Title | title }}</a></li>
    {{- end }}
    </ul>
{{- end }}
{{- end }}
</aside>

This snippet outputs something like this:

Tags:

  • Tag 1
  • Tag 2

Categories:

  • Category 1

ASurpriseTaxonomy:

  • Test Term

The first snippet would have ordered these ASurpriseTaxonomy, Catagories, Tags. But since tags and catagories are in $preferredTaxonomies, they appear first in that order.

Again: Not the nice clean solution you were looking for. (some sort of .Page.GetTaxonomies would be awesome), but ¯\_ ツ _/¯ it’s something I guess.