<$paginator.TotalPages>: error calling TotalPages: runtime error: invalid memory address or nil pointer dereference

something is wrong with .Paginator.TotalPages?
(With: Hugo Static Site Generator v0.59.1-D5DAB232 openbsd/amd64 BuildDate: 2019-10-31T15:22:06Z)

I wrote a pagination.html into partial folder, called by list.html, as:

{{ partial "pagination.html" . }}

The code, into pagination.html, is:

{{ $page := .Paginator }}
{{ if gt $page.TotalPages 1 }}
 …
{{ end }}

When I launch the server, I have this error:

Error: Error building site: failed to render pages: render of "taxonomy" failed: execute of template failed: template: _default/list.html:15:15: executing "main" at <partial "pagination1.html" .>: error calling partial: "~/hugo/blog/layouts/partials/pagination1.html:2:14": execute of template failed: template: partials/pagination1.html:2:14: executing "partials/pagination1.html" at <$page.TotalPages>: error calling TotalPages: runtime error: invalid memory address or nil pointer dereference

I change the first line as: {{ $page := $.Paginator }} with same error.

I dont understand the reason. Why?!

Hi there,

It’s difficult to say what’s wrong without seeing the rest of your code. Do you have a repo somewhere we can have a look at?

1 Like

Sure: Sign in · GitLab

but, it will not help you, because my recent tests to add pagination are not published;
just pagination.html is visible.

And, as I wrote on my first post, I call this partial; I modify my config.toml to add [params.pagination]!

I can’t help you if I can’t reproduce your issue.

The error message invalid memory address or nil pointer dereference suggests you are trying to access a nil value. Perhaps a nil .Paginator. So, my suggestion would be to check that you are passing the correct context to the partial. Maybe wrap the .Paginator code in a with to make sure it exists before trying to access .Paginator.TotalPages.

Yes, I understand!

Egual!

Seriously?
I try to understand why it’s not run. And I wrote exactly what I modified to attempt to use ‘paginator’.

1/ modification config.toml to add: [params.pagination]
2/ modification into partials/list.html, as:
<footer>{{ partial "pagination.html" . }}</footer>
3/ the pagination.html into partials, as view abowe.

I try to apply what Hugo doc say, but I have not good results!
Since I’m using Bootstrap, I even tried to add that:
{{ template "_internal/pagination.html" . }}
as it’s wrote into doc pagination, in first line into my partial pagination. With same bad result!

There’s something I’m not understand; certainly because of the “language barrier”, but what?!

What are you trying to do here? Anything under params in config.toml is custom parameters. It is not part of the official pagination documentation.

Where in list.html? If you are inserting this inside a range, for example, the dot would have a different value.


Like I said above, the error message implies that you are trying to access a nil value. So wrap your code in a with block to make sure it evaluates only if it is not nil.

Your pagination partial has a few more errors, where you use .Permalink (see your line 6-8 as an example).

{{ with $page.First }}
  ... {{ .Permalink }} ... <!-- wrong -->
{{ end }}

.First is a Pager, not a .Page. It does not have a .Permalink; use .URL instead.

So put these together:

{{ $paginator := .Paginator }}

{{ with $paginator }}  <!-- wrap in a with block -->
  {{ if gt .TotalPages 1 }}
    {{ with .First }}
      {{ .URL }} <!-- use .URL instead of .Permalink -->
    {{ end }}
  {{ end }}
{{ end }}

and so on.

1 Like

Into this #tips-tricks !

OK, I’m going to test! :stuck_out_tongue:

And, thanks for your help and patience :smiley:


Are you agree, pagination is for section, as this one on my published site!?
If, OK. the code you showed me not returns result, on my localhost.

OK, I’ve understand where was my error.
I had include the partial into the range. Boo…

@pointyfar: thank you!

Now, my paginator run correctly (into my git repository).
But, I’m not understand how to modify my code to display n links to articles into my default list.html, to match with page number ?!

See:

Any suggestion?!

I’m not sure what this means. Could you give a specific scenario example?

Not exactly.

As you can see, on my git repository, my code is in two distincts parts.

  • into the partials list.html, I have a range to display all informations about articles (title, description, tags, etc).
  • and after the paginator, into footer html.

Normally, the paginator display only the numbers of pages, and permit to browse page after page. But, as I wrote my code, all articles are “published” - if the section had 10, or 30 articles, all informations about them are displayed, not segun the number page!

I known: I’m having trouble expressing in English what I mean!

I thought that using paginator would display x items according to the pagine variable (customized or not into config.toml; by default, 10). But, as I wrote, it’s separated!

How I can modify my code to display the x items segun page 1, page n?

Change this line:

{{ range .Pages.ByPublishDate.Reverse }}

to

{{ range .Paginator.Pages.ByPublishDate.Reverse }}
1 Like