Cannot get pagination to work: invalid memory address or nil pointer dereference

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 }}

Guess: Your paginator is nil because there is nothing to paginate. Set your posts per page config to maybe 1 or 2 and see the paginator being filled.

After that, you can’t range over nil, so I would expect that second error.

You should work with with.

  {{ $paginator := .Paginate $pages }}
  {{ with $paginator }}
  {{ range .Pages }}
    {{ partial "post_card.html" . }}
  {{ end }}
  {{ end }}

@davidsneighbour - Thanks for your quick reply, that’s very helpful!

I added the change you suggested and it worked properly. Oddly enough, I then tried reverting to the original code and it still worked, which suggests that stopping and restarting the Hugo server may have been the actual underlying cause of the issue. I’ll have to make a mental note to try that whenever running into tricky issues again in future!

It depends on how you run hugo server. Some of the CLI arguments result in changes only to stuff you just edited. I tend to restart the server if the problem in the CLI is an ERROR. If it’s a WARN Hugo should be able to continue, but in my experience, an ERROR is something that can bring things out of synch somehow. Hugo recently started not failing completely on ERRORs. Before it would have stopped.