# Using taxonomies metadata in list and single templates

**URL:** <https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444>\
**Category:** support\
**Tags:** taxonomy\
**Created:** [April 19, 2021, 8:36pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444 "2021-04-19T20:36:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 19, 2021, 8:36pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/1 "2021-04-19T20:36:50Z")

</div>

I’m not familiar with Hugo data model and can’t find a working detailed tutorial (or just example site) on using additional taxonomies metadata in templates. That’s why question is probably silly.

Regular page front matter:

```auto
authors: "william-thackeray"

```

/content/authors/william-thackeray/\_index.md

```auto
fullname: "William Makepeace Thackeray"

```

list.html

```auto
<div class="meta-name">
  {{ $tagsLen := len .Params.authors }}
  {{ if gt $tagsLen 0 }}
      {{ range .Params.authors }}
      {{ . }}
      {{ end }}
  {{ end }}
</div>

```

as expected I see `william-thackeray`

The question is: what should be instead of dot (.) to output author’s fullname?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 19, 2021, 9:51pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/2 "2021-04-19T21:51:14Z")

</div>

```auto
<div class="meta-name">
  {{ $tagsLen := len .Params.authors }}
  {{ if gt $tagsLen 0 }}
    {{ range .Params.authors }}
      {{ (site.GetPage (printf "authors/%s" .)).Params.fullname }}
    {{ end }}
  {{ end }}
</div>

```

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 20, 2021, 8:09am UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/3 "2021-04-20T08:09:16Z")

</div>

It works. Thank you! So specific taxonomy metadata access performed via Page object with special name %taxonomy\_plural%/%term%  
The only tip I’ve already bumped up is that front matter in yaml

```
---
authors: "william-thackeray"
---

```

leads to error “can’t iterate”. To access range iterations it’s necessary to write in list form even with single author:

```
---
authors:
- william-thackeray
---

```

In latter case iteration works as intended

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 20, 2021, 10:25am UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/4 "2021-04-20T10:25:12Z")

</div>

Adjacent question arrived. Now my template code for rendering book tags looks like (simplified) this:

```
{{ range .Params.authors }}
    <a href="{{ printf "authors/%s" . }}">{{ (site.GetPage (printf "authors/%s" .)).Params.fullname }} </a>
{{ end }}

{{ range .Params.booktitles }}
    <a href="{{ printf "booktitles/%s" . }}">{{ (site.GetPage (printf "booktitles/%s" .)).Params.fullname }} </a>
{{ end }}

{{ range .Params.publishers }}
    <a href="{{ printf "publishers/%s" . }}">{{ (site.GetPage (printf "publishers/%s" .)).Params.fullname }} </a>
{{ end }}

```

It seems there must be a way to reuse anchor generation pattern via partial like

`{{ partial render-tag "authors" }}`

but I can’t find how to manage partial parameter to use as taxonomy id and as printf pattern (string) in this case. Is it possible?

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [April 20, 2021, 1:43pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/5 "2021-04-20T13:43:48Z")

</div>

May be something like

```auto
{{ $ctx := .ctx }}
{{$tax := .taxonomy }} 
{{ $string := printf "%s/%s" $tax $ctx }}
<a href="{{$string}}">{{ (site.GetPage($string)).Params.fullname }}</a>

```

calling it like

```auto
{{ partial "render-tag" (dict "ctx" . taxonomy "author") }}

```

Although I’m not sure that using “%s” to format an author’s full name will result in a valid URL here. E.g. “William Makepeace Thackeray” would result in

```auto
<a href="authors/William Makepeace Thackeray">

```

with unescaped spaces. Maybe `urlize` helps.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 20, 2021, 3:03pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/6 "2021-04-20T15:03:11Z")

</div>

> [@fairplay](#):
>
> taxonomy metadata access performed via Page object with special name %taxonomy\_plural%/%term%

There’s nothing special about the name. It’s just the path to the page.

> [@fairplay](#):
>
> it’s necessary to write in list form even with single author

You should always specify taxonomy terms in a slice, even if there is only one.

> [@fairplay](#):
>
> In latter case iteration works as intended

Good, but there’s a simpler, more defensive way to iterate through the authors. Your empty slice check will throw an error if the page doesn’t contain `authors` in front matter. You don’t need to check for an empty slice—the `range` command does that for you.

```auto
<div class="meta-name">
  {{ range .Params.authors }}
    {{ (site.GetPage (printf "authors/%s" .)).Params.fullname }}
  {{ end }}
</div>

```

> [@fairplay](#):
>
> there must be a way to reuse anchor generation pattern

Yes, and @chrillek has described the concept.

layouts/partials/render-tag.html

```auto
{{ $taxonomy := .taxonomy }}
{{ with .ctx }}
  {{ range index .Params $taxonomy }}
    {{ $p := site.GetPage (printf "%s/%s" $taxonomy (urlize .)) }}
    <a href="{{ $p.RelPermalink}}">{{ $p.Params.fullname }}</a>
  {{ end }}
{{ end }}

```

And call it with:

```auto
{{ partial "render-tag.html" (dict "ctx" . "taxonomy" "authors" )}}

```

Is there a reason that you can’t use the `.Title` instead of `.Params.fullname`? I ask because it seem repetitive. Instead of this:

```auto
---
title: "William Thackeray"
date: 2021-04-20T07:04:48-07:00
draft: false
fullname: "William Makepeace Thackeray"
---

```

Why not this?

```auto
---
title: "William Makepeace Thackeray"
date: 2021-04-20T07:04:48-07:00
draft: false
---

```

Then your partial would look like:

```auto
{{ $taxonomy := .taxonomy }}
{{ with .ctx }}
  {{ range index .Params $taxonomy }}
    {{ $p := site.GetPage (printf "%s/%s" $taxonomy (urlize .)) }}
    <a href="{{ $p.RelPermalink}}">{{ $p.Title }}</a>
  {{ end }}
{{ end }}

```

This would reduce data entry and the potential for errors and missing information.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 20, 2021, 3:49pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/7 "2021-04-20T15:49:08Z")

</div>

One more thought, just in case you were not aware. You can use spaces and capital letters in your terms. For example:

```auto
authors:
  - "Michael Moore"
  - "Mandy Patinkin"
booktitles:
  - "Fired"
  - "No Where But Up"
publishers:
  - "Greenway"
  - "Lexington Press"

```

---

<div class="post-metadata">

**Author:** ![fairplay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fairplay/32/9884_2.png) [@fairplay](https://discourse.gohugo.io/u/fairplay)\
**Post date:** [April 20, 2021, 8:04pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/8 "2021-04-20T20:04:22Z")

</div>

Yes, you’re right. And even quotes are not necessary with this type of front matter. But some books must be with non-ASCII letters (yes, I know, removePathAccents=true) including cyrillic authors, titles and publishers. It’s not convenient to process and send urls like **authors/лев-толстой**. Hugo AFAIK still has no tools to make cyrillic transliterations like **authors/lev-tolstoi** and transparently work with them. So, after rigorous brainstorming 🙂 I decided to use transliterated URLs as taxonomies with metadata.

Once more - thank you for detailed answers. They really not only helping realise my current humble project, but makes me better understand real power of Hugo.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [April 24, 2021, 7:11pm UTC](https://discourse.gohugo.io/t/using-taxonomies-metadata-in-list-and-single-templates/32444/9 "2021-04-24T19:11:36Z")

</div>

Closed on April 24, 2021, 7:11pm UTC

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