Access page data in a pager?

I’m using a custom pagination template basically like so

{{ $pag := $.Paginator }}
{{ range $pag.Pagers }}
...
 <a class="pagination-link {{ if eq . $pag }}is-current{{ end }}" 
        href="{{ .URL }}">{{ .PageNumber }}</a>
...
{{end}}

and it does what I want.

However, I’d like to include some information in the title of the a elements, something that can be retrieved from the front matter of the pages in this pager.
If I understand the documentation correctly inside the range, .Pages should give me a (what - slice? map? something else?) of the pages in the current pager. According to warnf #%v I do get something that’s a list/slice/something of Pages.page objects, and I can get for example the first element of it with first 1 .Pages. However, this does not seem to be a page, since trying to access its front matter variables with .Params.Variable throws an error:
can't evaluate field Params in type page.Pages
Is there a way to get at the pages (or their URLs or anything that gets me a handle to them) in a pager?

Hmm, I saw this post and wondered if somebody would chime in to politely explain that it’s not possible. But nobody answered yet, so I try to add my two cents. Not possible :slight_smile:

There is no way to access page data inside of a pager without additional work and it makes no sense to add this by default with internal tools, because it’s a pager, not a pag(e-rang)er. It’s function is navigation, not content.

But: You could in your range use the getPage function, to get the page in question and from there add the parts.

The problem here is, that this will probably lead to very long build times. If you 100% need it, then live with a coffee break every now and then and use getPage inside of the range.

Another possible solution might be something along the line of not using pagination but ranging through your pages again and create your own “pager” by using something like range (where .Pages "Type" "posts"), but that too will increate the build time massively.

If you want to see whats inside your variable you could use a tool like debugprint by @kaushalmodi that iterates through your dot to see what is included.

1 Like

Thanks for chiming in. I already suspected that it’s not possible. So I’ll give .GetPage a try – there are not so many pages anyway, so I could live with a coffee break now and then.
Though I’m not clear on how to get the to path for .GetPage from inside the pager. The .URL field is, as far as i can see, something like “…/page/3”, whereas I’d need “…/blog/post.html” or so.

Yeah, that’s why I added the debugprint note. I think you need to start with “debugging the dot” inside of a pagination range and see what you get. I am not sure what is included, because all I need is a label and a n URL up to this post :slight_smile:

So,

  • .Paginator and .Paginate returns a list of “paginator pages”
  • Each paginator page has some methods, including HasNext and … Pages.

WIth that, it does not make sense to call .Params on a “paginator page”, because it does not have params…

So:

{{ range .Pages }}
// This is a paginator page, e.g. a group of pages
{{ range .Pages }}
// This is the page
Param: {{ .Param.foo }}
{{ end }}
{{ end }}
2 Likes

I should have known it’s already thought of.

The inner range .Pages does not seem to contain anything. Neither does warnf %#v . print anything nor do I get any output from Param: {{.Param.Title}}. The context in the partial is this:

{{ $pag := $.Paginator }}
{{ range $pag.Pagers }}
    {{ range .Pages}}
    {{ range .Pages}}
    {{ warnf "%#v" .pageCommon}} <!-- no output -->
    Param {{ .Param.Title}} <!-- not output either -->
    {{end }}
    {{ end }}
... 
{{end}}

You are ranging pages twice. Why? You are aware of what the dot . means inside of a range?

I would epxect that to … fail the build.

I was merely repeating what @bep suggested here. I was wondering about the duplication of ranges, but supposed it was intended since there are also to end statements.
Yes, I’m aware of "what the dot means.
Investigating further, I get this to work a bit:

{{ range .Pages }}
    Param {{ .Param "country" }} <!-- works ok -->
    {{ end }}
    {{ $pag1 := first 1 .Pages}}
    {{ warnf "%s" ($pag1.Param "country") }} <!-- error -->

The Param ... part does what it’s supposed to do. However, attempting to get at the first of the .Pages and then access the .Param gives an error:

 at <$pag1.Param>: can't evaluate field Param in type page.Pages

Same thing when using with (first 1 .Pages) and .Param ....
But range first 1 .Pages is just fine…