Hugo

Extracting taxonomies from page frontmatter

Let’s assume there are taxonomies defined in config.toml like this:

[taxonomies]
    taxonomy(1)             = "taxonomies(1)"
    taxonomy(2)             = "taxonomies(2)"
    taxonomy(3)             = "taxonomies(3)"
    taxonomy(4)             = "taxonomies(4)"
    taxonomy(5)             = "taxonomies(5)"
    taxonomy(6)             = "taxonomies(6)"

and each of them has some terms associated.
Now in a content page we have a frontmatter like this:

+++
date           = "2019-12-31T09:07:05+02:00"
title          = "some interesting page"
description    = "this is awful stuff"
taxonomy(2)    = ["term(21)", "term(24)"]
taxonomy(5)    = ["term(52)", "term(53)"]
+++

In a single page template for that section I want to get the taxonomies and their associated terms used in the particular page which of course is a subset of the taxonomies defined in the config.toml.

So for the page above I want to get that taxonomies 2 and 5 with their terms 21 and 24 or 52 and 53 respectively.

Goal is to do something like:
"More articles in [taxonomy(2)](link-to-taxonomy(2)-list-page) about the subjects [term(21)](link-to-term(21)-list-page) and [term(24)](link-to-term(24)-list-page)"
somewhere on each single page.

I could not figure out how to do that. I only found explanations on how to do some sort of navigation based on all of the taxonomies from the config.toml

Have you figured out how to show all the terms in a single taxonomy for a piece of content?

not yet.
I’m still trying to start at the beginning: figure out how to find out about the taxonomy itself.

Use the samples in the docs and study the themes in the showcase; examples of taxonomy use abound.

In the docs I simply can’t find anything that could apply to content pages.
In list pages I simply have

        <ul>
            {{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
            {{ if ne $taxonomyname "main" }} 
              <li>
                <a href="{{ "/" | relLangURL}}{{ $taxonomyname | urlize }}.html">{{ $taxonomyname | upper }}</a> <!-- "| upper"  oder "| humanize"  -->
              </li>
            {{ end }}
            {{ end }}
        </ul>

to show a list of taxonomies - but in a content page I only want to show the taxonomies assigned to that page.

Further:
This https://gohugo.io/templates/taxonomy-templates/#display-a-single-piece-of-contents-taxonomies skips the first step i need: finding out which taxonomies exist on this page.
The examples assume “tags” or “directors” - but I don’t know that in advance in the template, because that is defined in frontmatter of the respective content page.

Meanwhile I’ve come up with the following approach:

  1. range through the site-wide taxonomies as above
  2. check for each of them, if .Param.[taxonomyname] exists and is set on the current content page
  3. if yes => go further with extracting the content of the existing .Param.[taxonomyname] => these should be the needed terms.

But in practice I’m not able to do step 2.
I cannot find a way to do something like isset in other languages (at least not one that I understand).

I have a taxonomy called bildbearbeitung in frontmatter with

+++
bildbearbeitung= ["RAW"]
+++

If I do

    {{ if .Params.bildbearbeitung }}
        <p>bildbearbeitung exists</p>
    {{ end }}

Hugo prints out the sentence.
With

    {{ if .Params.foo }}
        <p>bildbearbeitung exists</p>
    {{ end }}

output is empty obviously as there is no such taxonomy.

Now I would like to do this:

<ul>
{{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
    {{ if .Params.$taxonomyname }} 
        <li>
            <a href="{{ "/" | relLangURL}}{{ $taxonomyname | urlize }}.html">{{ $taxonomyname | upper }}</a> <!-- "| upper"  oder "| humanize"  -->
        </li>
    {{ end }}
{{ end }}
</ul>

but Hugo tells me:

Process: loading templates: “/home/path/to/theme/layouts/partials/pagetax.html:5:1”: parse failed: template: partials/pagetax.html:5: bad character U+0024 ‘$’

So how can I do that if-statement {{ if .Params.$taxonomyname }} (this is the cause fo the error) right?

I cannot believe that noone seems to know the correct syntax of concatenating strings in a template :thinking:

I’m still struggling with this.
As mentioned in step 2 I have to check the existance of a frontmatter parameter. This is the taxonomy as described in the first entry of this discussion .

So I still try to set a variable, let’s say $taxcheck as a combined string out of ".Params." and the variable $taxonomyname but I cannot figure out how to do this :confused: .

Next thing then would be to do an if-statement to check if

  • either this concatenated variable exists in the page frontmatter
  • or, if not possible, if this concatenated variable is not empty

After reading this https://gohugo.io/templates/introduction/#example-4-if--else again I tried config.toml

[taxonomies]
    foo             = "foos"
    bar             = "bars"
    baz             = "bazs"

frontmatter

+++
date           = "2019-12-31T09:07:05+02:00"
title          = "some interesting page"
description    = "this is awful stuff"
bar            = ["bar1term", "bar2item"]
+++

and this partial

<ul>
{{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
    {{ if (isset .Params $taxonomyname) }} 
        <li>
            Taxonomy is {{ $taxonomyname }}
        </li>
    {{ else }}
        <li>{{ $taxonomyname | upper }} not found</li>
    {{ end }}
{{ end }}
</ul>

but I always get the “not found” statement - which I would be expecting to be correct for foo and baz taxonomy, but not for bar.

I don’t create general use themes that produce taxonomies, so I’m not sure of the exact code I’d use to check.

In my practical case, I generate a layout for each taxonomy as its added to my project, so I can control that aspect. From your original goal, you know what the taxonomies are, to produce the human readable text. That text depends on a human understanding the relationship between the taxonomies.

What you seem to want is a name of the taxonomies used in the front matter of a given piece of content, for it’s own sake (to operate on later). I don’t think many folks have that goal in mind. You know: you set the taxonomies in front matter, how do you not know that they are? :slight_smile:

Anyhow, if you needed to know which taxonomy a piece of content has, you can compare the parameters in the front matter to .Site.Taxonomies, and maybe use intersect to produce your list. Or something. :sunglasses:


Also, sharing a repo to clone to reproduce your work goes a long way.