Displaying post counts on the Hugo tags page
Cleaning up and adding some information to the tags page (/tags).
Related:
The original (default) taxonomy.html page
Other than wrapping the output in a .tags div for styling (and displaying .Name instead of .LinkTitle), this is the default taxonomy.html template that was generated with hugo theme new <theme-name>:
{{ define "main" }}
<h1>{{ .Title }}</h1>
<div class="tags">
{{ .Content }}
{{ range .Pages }}
{{ warnf "Debug: context = %#v" .}}
<h2><a href="{{ .RelPermalink }}">{{ .Name }}</a></h2>
{{ end }}
</div>
{{ end }}
Since I’m not using category based taxonomy on the site, I think the only relevant taxonomy is
what’s found at /tags. (TODO: remove the empty /categories directory from the public directory.)
Adding post counts to the tags page
{{ define "main" }}
<h1>{{ .Title }}</h1>
<div class="tags">
{{ .Content }}
{{ range .Data.Terms.ByCount }}
<h2><a href="{{ .Page.RelPermalink }}">{{ .Page.Name }} ({{ .Count }})</a></h2>
{{ end }}
</div>
{{ end }}
Why it works
The first thing to look at is the current context (.):
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ warnf "Cursor %v" .}}
<div class="tags">
{{ .Content }}
{{ range .Data.Terms.ByCount }}
<h2><a href="{{ .Page.RelPermalink }}">{{ .Page.Name }} ({{ .Count }})</a></h2>
{{ end }}
</div>
{{ end }}
Outputs:
WARN Cursor /categories
WARN Cursor /tags
My understanding is that the cursor (.) in this case represents first the “categories” Page,
then the “tags” Page.
Each Page has a Data field:
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ warnf ".Data %v" .Data}}
The Data field is a map of maps (note that I don’t know much about Go, so I’m guessing about
Index:map):
.Data map[Index:map[ai:[WeightedPage(0,"Notes on Cognitive and Morphological Patterns")]
There’s a map (list, array(?)) for each of the tags page’s tags (“ai”, “alchemy”, “bacon”, etc):
alchemy:[WeightedPage(0,"Alchemy Restored")] bacon:[WeightedPage(0,"Alchemy Restored")
WeightedPage(0,"Roger Bacon as Magician") WeightedPage(0,"Scientia Experimentalis")
WeightedPage(0,"Who Was Ibn al-Haytham?") WeightedPage(0,"Roger Bacon Resources")
WeightedPage(0,"Roger Bacon's Family Life") WeightedPage(0,"University of Oxford")
WeightedPage(0,"Roger Bacon and the Brazen Head") WeightedPage(0,"Roger Bacon Overview")]
.Data.Terms is a “cleaner map” (it exists in the data returned from .Data):
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ warnf ".Data.Terms %v" .Data.Terms}}
.Data.Terms map[ai:[WeightedPage(0,"Notes on Cognitive and Morphological Patterns")] alchemy
[WeightedPage(0,"Alchemy Restored")] ...
.Data.Terms.ByCount gives a new type of data structure (TODO: notes / learn about Go data
structures.)
.Data.Terms.ByCount [{coding [WeightedPage(0,"Remove Categories Route From Site")
WeightedPage(0,"Displaying Post Counts on the Hugo Tags Page")
WeightedPage(0,"Printing Debug Information From Hugo Templates to the Terminal")
What seems significant is that the objects (?) in the ByCount field have a Count method. And
the field can be iterated over with the range function.
With a bit of CSS, I’ve almost made a tag cloud. It seems useful though: