Using taxonomies with a single value

I am using the blogging system to collect the presentations I’ve given. A presentation can have one or more co-speakers and a country code (from where the presentation was given).

It looks like taxonomies do no work if they’re defined as a single value. This makes sense given what they’re supposed to do but I am wondering if there was a way around that or if I was just hacking the wrong concept.

Ultimately, I’d like to have something like /country/BE that lists all the presentations I’ve given in Belgium. While the co-speakers one works, it would be convenient to be able to specify a single value (as this is by far the most common setup, I only have one presentation with 2 co-speakers).

Thanks!

Taxonomies work just fine with a single value.

Hum. I meant defined as a single value, something like

country: "BE"

vs.

country: [ "BE" ]

Either way is fine.

Damn, I think it’s my own mistake :confused:

I am doing this in a template to display the co-speaker(s):

{{ range $index, $el := .speakers }}

which leads to:

execute of template failed at <.speakers>: range can’t iterate over bclozel

I guess I need to have some sort of special case if the value isn’t a range or something?

Which template?

Are co-speakers the same as speakers (your taxonomy)?

Yeah sorry. I found that coSpeakers wasn’t a good name so I just went with speakers for the taxonomy.

I have a template that loops over the co-speakers and add them in the summary view using a partial. This looks like this:

<span>
  {{ $dict := .speakersDict }}
  {{ range $index, $el := .speakers }}
    <!-- Replace certain special characters with their URL encoded counterparts -->
    {{ $item := replace . "#" "%23" }}
    {{ $item = replace $item "." "%2e" }}
    {{ $link := ( printf "%s/%s/" "speakers" ( $item | urlize ) ) | relLangURL }}
    {{ $speakerData := index $dict . }}
    <a class="speaker" href="{{ $link }}">{{ $speakerData.name }}</a>
  {{ end }}
</span>

Where the partial is invoked as follows:

{{ with .Page.Params.Speakers }}
  {{ partial "taxonomy/speakers.html" (dict "speakers" . "speakersDict" $.Site.Data.speakers) }}
{{ end }}

Thanks!

Why are you ranging over .Page.Params.speakers when it only contains a string?

Also, I think this construct would simplify things for you:

  {{ with .GetTerms "speakers" }}
  <p>Speakers:
    {{ range . }}
      <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
    {{ end }}
  </p>
  {{ end }}

.GetTerms is a method on page.

1 Like

That’s the whole point of my question. I think we need to reset.

A presentation can have one or more speakers (as I’ve stated in the first message). So this can be:

speakers: "foo"

or

speakers: [ "foo", "bar" ]

Essentially, I am trying to be lenient. Perhaps I need some sort of if/else case?

Look at the .GetTerms method above. It doesn’t care about scalar vs array.

Also, you don’t have to store meta data about each speaker in a data base.

hugo new speakers/john-doe/_index.md

front matter

+++
title = 'John Doe'
age = 42
department = 'Mathematics'
+++

Awesome, I had no idea this was handled this way!

Back to my original question, I have this in my front matter:

---
country: BE
---

And this in my taxonomy:

taxonomy:
  "country": "country"

This feels a bit off a there must be some sort of plural there but I effectively only have one country ever for a presentation. Any advice?

No, that’s fine.

And here’s an example site if you need one, based on our conversation.

git clone --single-branch -b hugo-forum-topic-42441 https://github.com/jmooring/hugo-testing hugo-forum-topic-42441
cd hugo-forum-topic-42441
hugo server

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.