On my homepage, I am trying to display a short piece of introductory content followed by a paginated list of posts.
I have followed the pagination guidance in the official Hugo docs here and have spent hours experimenting and searching for answers, but I am unable to use the pagination successfully.
If I do not use pagination and use the following code as my layout’s root index.html
, everything works fine and I get a list of all of my posts (I currently have 4 posts in my project). NB: I have not included the content of the partial, as the error described below occurs before reaching the partial:
{{ define "main" }}
{{ .Content }}
{{ range (where .Site.RegularPages "Type" "in" "posts").ByDate.Reverse }}
{{ partial "post_card.html" . }}
{{ end }}
{{ end }}
However, as soon as I try to paginate the above pages, I run into an error. Please see below for the code which I am using, together with the output of several debugging printf
statements.
Can anyone tell me what I am doing wrong? NB: I have tried both {{ range $paginator.Pages }}
and {{ range $paginator }}
but neither works, seemingly because the paginator is itself nil.
{{ define "main" }}
{{ .Content }}
{{ $pages := (where .Site.RegularPages "Type" "in" "posts").ByDate.Reverse }}
{{ printf "%#v" (len $pages) }} <!-- correctly shows 4 -->
{{ printf "%#v" (index $pages 0).Content }} <!-- correctly shows content of first page -->
{{ $paginator := .Paginate $pages }}
{{ printf "%#v" $paginator }} <!-- shows "(*page.Pager)(nil)"; this is not what I would expect! -->
{{ range $paginator.Pages }} <!-- gives compilation error: "execute of template failed: template: index.html:11:21: executing "main" at <$paginator.Pages>: error calling Pages: runtime error: invalid memory address or nil pointer dereference" -->
{{ partial "post_card.html" . }}
{{ end }}
{{ end }}