Access term metadata from within .Data.Terms.ByCount loop

I’m working on authors pages for a site. I want the authors/ page to show all authors ordered by number of posts, and I want to see their names instead of the slugified/urlized version, so I’m using this in terms.html:

  {{ range $key, $value := .Data.Terms.ByCount }}
  {{ $name := $value.Name | humanize  }}
      <a href="{{ $value.Name | urlize }}">{{ $name | title }}</a>
      <span>&#40;{{ $value.Count }}&#41; </span>
  {{ end }}

My problem is that even the humanize+ title combination doesn’t give me the actual names, e.g. Jane McDonald becomes Jane Mcdonald. How could I access the Params.name of each terms page from within this loop? I’ve tried various things without success.

What does $name return, without processing it through title? Should just be Jane McDonald, correct?

Edit: what I mean is, you are transforming the person’s name for the URL, and then trying to turn it back. But you can create those two values separately, rather than one based on the other.

1 Like

No, the name here is by default jane-macdonald hence my using humanize and then title. I guess my using urlize for the href is unneeded.

A workaround I’ve thought of is hardcoding a replaceRE for the few authors with a name with a capital letter inside.

Ah, okay. Well, um, why not save it as a normal name, and then do your transformations? I do that. I think a lot of folks do that. :slight_smile:

I am not sure I understand your suggestion, do you have any example?

Actually, you should be providing the examples. :slight_smile:

Please read Requesting Help and share your code. At the very least knowing what your content files are holding would help.

If I have a taxonomy term with a name, I’d save it as the title for the term. So you’d have:

---
title: Jane McDonald
---

I presume you are saving the name as a value in the format you want, so I am suggesting you access it as such. Is my presumption incorrect?

1 Like

Sorry and thanks, now I see what you mean by name. The metadata files for authors (in content/authors) have a name… but not a title so I’ll experiment with that, in hindsight name was a bad parameter name!

content/authors/jane-mcdonald.md

--- 
name: Jane McDonald
twitter: janemcdonald
---

And in content/blog

--- 
slug: is-invasive 
title: is.invasive() 
date: '2012-11-26' 
authors: 
  - Jane McDonald
--- 

From within the loop

{{ range $key, $value := .Data.Terms.ByCount }}
 {{ printf "%#v" . }}
{{ $name := $value.Name | humanize }} 
<a href="{{ $value.Name | urlize }}">{{ $name | title }}</a> <span>&#40;{{ $value.Count }}&#41; </span> 
{{ end }}

when printing variables in scope (see above) I only got the term name (“jane-mcdonald”) and the pages where she is an author, not anything related to the metadata, but I’ll try again with title :slight_smile:

If needed I’ll post something minimal because the website is not small. https://github.com/ropensci/roweb2/tree/authors/content

Thanks again!

I would be a lot more helpful if you created a tiny site to clone. :slight_smile:

1 Like

Here is the repo at last :slightly_smiling_face:

https://github.com/maelle/multiauthors-site (adapted from https://github.com/imorente/hugo-multiauthor-example/)

And the corresponding deployed authors page https://5c32f4a29066928a5b050341–focused-haibt-674433.netlify.com/authors/ where I printed the variables in scope, terms name and pages authored by the author corresponding to the term.

From within the loop, I don’t have access to authors metadata (even after adding title).
If I replace .Data.Terms.ByCount with .Data.Pages I do have access to the authors metadata, so it’s a feature of .Data.Terms I suppose. What I’d like is the best of both, the ordering by count and the count, and all the metadata.

My current best guess is looping over all website pages for each term, to find the corresponding author page.

I nearly got it working but encoding got me.

what nearly works for finding the page corresponding to a term

{{ range $key, $value := .Data.Terms.ByCount }}
  {{ $name := $value.Name }}
  {{ $authors := print "/authors/" $name "/" }}
 {{ printf "%#v" $authors }}
  {{ range first 1 (where $.Site.Pages "RelPermalink" $authors) }}
  {{ $title := .Params.name }}
      <a href="{{ $name | urlize }}">{{ $title }}</a>
      <span>&#40;{{ $value.Count }}&#41; </span>
  {{ end }}
  {{ end }}

I only nearly works because the RelPermalink variable has a different encoding so for Jäne McDonald it’s /authors/j%C3%A4ne-mcdonald/ whereas the name I’m comparing it to is /authors/jäne-mcdonald/ See current state of https://github.com/maelle/multiauthors-site and preview at https://5c3306a1eed26100078411b6–focused-haibt-674433.netlify.com/authors/

I’m giving up for now, I hope I’m only missing an obvious way to access authors metadata from within .Data.Terms.ByCount.

Problem solved, the solution was easy indeed, I re-read https://gohugo.io/content-management/taxonomies/ and found https://gohugo.io/content-management/taxonomies/#preserve-taxonomy-values

Not in the config I set preserveTaxonomyNames = true

And in terms.html I have

  {{ range $key, $value := .Data.Terms.ByCount }}
  {{ $name := $value.Name | urlize }}
  {{ $count := $value.Count }}
      <a href="{{ $name }}">{{ $value.Name }}</a>
      <span>&#40;{{ $count }}&#41; </span>
  {{ end }}

and all is well! :tada:

I had actually seen the option yesterday but in the context of https://github.com/gohugoio/hugo/issues/2835 so I thought it no longer existed! After looking at the Hugo repo I see it was then re-added.

I guess this solves my problem by making it useless for me to access term metadata from within .Data.Terms.ByCount loop, but if I were to need the Twitter username of authors in terms.html, I’d use $Site.GetPage which is also something I had missed earlier.