Possible to run .Paginator in Menu?


I’m building a off-canvas menu for my website. I was trying to include paginated list pages also in the menu.

Here is my current navigation partial that’s working fine, however, without any automatic addition of paginated pages: https://github.com/Hrishikesh-K/Portfolio/blob/v2/layouts/partials/navigation.html

Here’s its working demo: https://v2--hrishikeshk.netlify.app/projects/ (click on the menu icon in the top left).

So, as you can see in that, I have my section ‘Projects’ as the parent under which I have a link to the projects page itself (the parent has to expand/collapse, so, I can’t just link that to the page directly), followed by the links to other posts.

However, I want the ‘All posts’ to turn into another parent with a list of pages as its child when my posts count increases beyond my Pagination value (current is the default: 10).

This is what I tried:

At line 39, I added this:

{{ $totalPosts:= len (where $.Site.RegularPages "Section" (lower .Name)) }}
{{ $totalPages:= math.Ceil (div $totalPosts $.Paginator.PageSize) }}

{{ if gt $totalPages 1}}

  {{ range $i:= (seq 1 $totalPages) }}

    <!-- code for each <li> -->

  {{ end }}

{{ end }}

With that, I get the error: <$.Paginator.PageSize>: error calling PageSize: runtime error: invalid memory address or nil pointer dereference which probably means that I can’t use .Paginator inside my Menu range. But then is there any alternative that I can use? I don’t want to hardcode the page size value as 10 in the template (it works, by the way).

So, I tried to set the value using .Scratch. This is what I did:

In my section template that’s calling the partial and already has a paginator setup:

{{ .Scratch.Set "paginatorSize" $paginator.PageSize }}

Then, in the navigation partial, I tried a lot of combinations to get the division working, but, I keep getting the error: error calling div: can't apply the operator to the values.

Then, before ranging in my navigation partial, I added this:

{{ $paginatorSize := .Scratch.Get "paginatorSize" }}

This is one of the attempts I made to divide the values:

{{ $totalPages:= math.Ceil (div (printf "%v.0" $totalPosts) (printf "%v.0" $paginatorSize)) }}

You are using your navigation partial on a single.html layout, which has no Paginator, which gives you the nil pointer error.

Alright, so, if I can’t use .Paginator, what other option do I have?

Like the option I tried in my reply, I could get value from the page that’s getting paginated and use it to do the calculation, but, it’s giving an error too.

Is there any other way?