Missing .Data.Terms in taxonomy.html and terms.html templates?

Hi folks,

I’m working on a template to list the tags associated with various blog posts on my Hugo site. I’m using Hugo 0.101.0 from Homebrew:

hugo v0.101.0+extended darwin/amd64 BuildDate=unknown

I’ve created a template file at layouts/_default/taxonomy.html and written the following:

<ul>
{{- range $term, $page := .Data.Terms.Alphabetical -}}
<li>{{ $term }} -- {{ $page }}</li>
{{- end -}}
</ul>

Only an empty <ul> is output. I tried printing out <p>{{ .Data }}</p> and I get the following:

map[Index:map[] OrderedIndex:map[] Plural:tags Singular:tag Terms:map[] pages:0x512a200]

It appears the Terms map is empty, despite having plenty of tags on my site. When I make a list of .Pages, I get a list of pages for each tag, as I expect. I’ve also noticed the same behavior if I put this template at layouts/_default/terms.html. Am I doing something wrong here?

Side note: I’ve also noticed that .Title doesn’t output anything in this template. Is that expected? I’ve been using {{ .Data.Plural | title }} to get my page title.

Thanks in advance!

You would typically iterate over .Pages in taxonomy and term templates. For example…

layouts/_default/taxonomy.html

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
{{ end }}

layouts/_default/term.html

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
{{ end }}

Is there some reason this won’t work for you?

Thanks. No reason why I couldn’t use .Pages. Most of the examples in the docs show templates iterating over .Data.Terms. If that’s not the correct guidance for terms templates, it’d be great if the docs could be updated to clarify that point. :slight_smile:

There is one example of iterating over .Pages but the title of that bit is “Displaying custom metadata in Taxonomy Terms Templates” which didn’t seem to be what I was looking for.

I also thought it was odd that .Title wasn’t working correctly, since again several of the examples in the docs use it for the page title, and all my other templates use it. Thought that might be a configuration issue in my site, or possibly a bug?

If you’re iterating over .Pages in the taxonomy template, use .Title to render the page title.

If you’re iterating over .Data.Terms in the taxonomy template, use .Page.Title to render the page title.

You can add something like this to the template to see the data structure:

<pre>{{ jsonify (dict "indent" "  ") .Data.Terms }}</pre>
1 Like

This is extremely handy, thank you!

jsonify will blow up if you try to use it with .Pages:

<pre>{{ jsonify (dict "indent" "  ") .Pages }}</pre>

So this approach is useful to inspect data structures, but you can’t use it everywhere.

Got a chance to try this, but it doesn’t work. I’m iterating .Pages to produce a list of tags, but .Title is empty. .Page.Title is also empty.

Here’s my template:

{{ define "main" }}
<header class="page">
  <h1>{{ .Data.Plural | title }}</h1>
</header>

<h2>Pages</h2>
<ul>
  {{- range .Pages -}}
  <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  {{- end -}}
</ul>

<h2>Terms</h2>
<pre>{{ jsonify (dict "indent" "  ") .Data.Terms }}</pre>
<ul>
  {{- range $term, $page := .Data.Terms.Alphabetical -}}
  <li>{{ $term }} -- {{ $page }}</li>
  {{- end -}}
</ul>

{{ end }}

Iterating .Pages gets me links, just like any list template, but I’m missing all the useful metadata about tags. Is there a way to get at term weights, counts, etc by iterating pages?

Everything under the Terms heading is empty.

Can you share a link to your project repository?

Sure: GitHub - erynofwales/erynwells.me: My website

Try to use the <pre>{{ jsonify ...}}</pre> approach to see the data structure, then remove it.

I am unable to reproduce this.

Not easily.

I am unable to reproduce this.

Does “unable to reproduce” mean you pulled my repo and can’t repro, or are you testing in your own sandbox?

I pulled your repo and removed the <pre>{{ jsonify ...}}</pre> lines.

I was testing layouts/_default/terms.html by visiting http://localhost:1313/tags/

Well, that’s weird! What version of Hugo are you using? Could it be something to do with how I’m running my dev server? I usually run ./dev-server.sh, which does this:

hugo server -DF --disableFastRender public

Is there anything else that could explain this?

The last word in the line above does nothing.

I am running v0.104.3.

Looks like…

Gotcha. Thanks.

I’m on a slightly older version. I’ll try updating. Your screenshot is how I would expect it to look!

Thanks for your help so far!

Some combination of upgrading hugo to 0.104.3 and restarting hugo server got it working!

@jmooring Thank you for your help with this!

1 Like

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