How to perform pagination under taxonomy.html?

I’m trying to paginate using following codes

{{ $maxPage := 20 }}
{{ $paginator := .Paginate .Data.Pages $maxPage }}

{{ range $paginator.Pages }}
{{ . }}
{{ end }}

{{ partial "pagination.html" . }}

Above code will throw me error like below:

execute of template failed –  is nil; wrap it in if or with: {{ with  }}{{ .Pages }}{{ end }} 

I’m not sure what’s happening. When I’m trying to loop without pagination, it works

{{ range .Data.Pages }}
{{ . }}
{{ end }}

I’m using the latest hugo version: hugo v0.148.1

Please help @bep, @jmooring and others, thanks!

I found the solution after following this past discussion.

Turns out, you need to explicitly declare the pagination in the range syntax like below:

{{ range (.Paginate .Data.Pages).Pages }}
{{ . }}
{{ end }}

I’m not sure about the the reason behind this. Is it because the variable make the paginator object static?

Your original code works fine. Try it:

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

Files of interest:

  • layouts/taxonomy.html

Ok, thanks @jmooring !

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