How can I get a PageList of taxonomy pages?

I currently get a list of taxonomy pages like this:

{{ range $name, $taxonomy := site.Taxonomies }}
    {{ $collections = $collections | append $taxonomy.Page }}
{{ end }}

However, I can’t do $collections.ByWeight, because the type is []interface{}, not PageList. I don’t see a way to get a PageList for taxonomy pages. Is there a way to do that? Is there a way to take any []interface{} and convert it to a PageList to use its group-by functionality?

I’m probably missing something, but I cannot reproduce the problem.

This code works great:

{{ $collections := slice }}
{{ range $name, $taxonomy := site.Taxonomies }}
  {{ $collections = $collections | append $taxonomy.Page }}
{{ end }}

{{ range $collections.ByWeight }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  Weight = {{ .Weight }}
{{ end }}

Try it:

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

Files of interest: layouts/_default/home.html

Commands:

hugo new site foo
cd foo
hugo new yourpost.md
hugo mod init github.com/carpikes/yourproject
hugo mod get github.com/willfaught/paige@v0.80.2
cat >>hugo.toml <<EOF
[[module.imports]]                      
path = "github.com/willfaught/paige"
EOF
hugo server -D

Output:

# ...snip...
Start building sites … 
hugo v0.127.0+extended darwin/arm64 BuildDate=2024-06-05T10:27:59Z VendorInfo=brew

Built in 164 ms
Error: error building site: render: failed to render pages: render of "home" failed: "/Users/will/Library/Caches/hugo_cache/modules/filecache/modules/pkg/mod/github.com/willfaught/paige@v0.80.2/layouts/_default/list.html:5:3": execute of template failed: template: _default/list.html:5:3: executing "main" at <partial "paige/pages.html" $page>: error calling partial: "/Users/will/Library/Caches/hugo_cache/modules/filecache/modules/pkg/mod/github.com/willfaught/paige@v0.80.2/layouts/partials/paige/pages.html:44:17": execute of template failed: template: partials/paige/pages.html:44:17: executing "partials/paige/pages.html" at <.ByWeight>: can't evaluate field ByWeight in type []interface {}

Instead of me digging through your theme, perhaps you could compare what you have done with the simple example above.

2 Likes

The post above is meant to show you that the code is the same.

The actual problem is that for a new site that has the default category and tag taxonomies left enabled, with a single default post that has no categories or tags defined in its front matter, the code $taxonomy.Page in the loop above returns nil, even though $name is “category” and “tag”. $taxonomy is page.Taxonomy{} for both. So the error can't evaluate field ByTitle in type []interface {} is wrong, because the issue isn’t the type []interface{}, it’s the fact that the slice elements are interface{}(nil), which it can’t call .Weight on.

I take your post above to mean that any []interface{} can work with .ByWeight.

I like your point about testing without a theme. I’ll try to do that going forward.

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