I have an issue with pagination being built on the homepage, even though no call for pagination is made in the template, except in the header as an if statement. Essentially, I can navigate to page/2/, page/3/ etc for all the pages.
{{ .Title }}{{if .Paginator}}{{if gt .Paginator.PageNumber "1"}} - Page {{
.Paginator.PageNumber }}{{end}}{{end}}
This small piece of code initializes the pagination. I would expect it not to. My intention is to merely check if there is pagination.
The code is fairly simple and self explanatory. Am I doing it wrong, or does this look like a bug?
I’ve circumvented this by having a unique base template for my blog section, and including the code there, instead of the default base template. But would like people’s thoughts on this. I’m tempted to submit a bug report.
https://gohugo.io/templates/pagination/#list-paginator-pages
For a given Page, it’s one of the options above. The .Paginator
is static and cannot change once created.
If you call .Paginator
or .Paginate
multiple times on the same page, you should ensure all the calls are identical. Once either .Paginator
or .Paginate
is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.
This describes the problem in a little more detail:
https://discourse.gohugo.io/t/determine-if-current-page-is-result-of-pagination/37494/4
This describes a strategy (with example) for doing what you want to do:
https://discourse.gohugo.io/t/control-pagination-and-page-collections-from-baseof-html/37643/8