Hugo

How Check if the Page have Pagination/Paginated

What I need is to put some meta tags in the <head> if the page paginated.

This is from my blog section layouts/blog/list.html

{{ $paginator := .Paginate (where .Data.Pages "Type" "blog") 7 }}
{{ range $paginator.Pages }} 
   ...
{{ end }}

<!-- paginator --> 
{{ $pag := .Paginator }}
{{ if gt $pag.TotalPages 1 }}
       {{ if .Paginator.HasPrev }}
           ...
        {{ else }}
           ...
        {{ end }}

       {{ if .Paginator.HasNext }}
           ...
        {{ else }}
           ...
        {{ end }}
{{ end }}

So i’m trying have the conditional (if page paginated) in my <head> with this code

{{ if .Paginator.HasPrev }}  //or {{ if .Paginator.HasNext }} 

the result is <.Paginate>: error calling Paginate: a Paginator was previously built for this Node without filters; look for earlier .Paginator usage

Any clue?

Could be the variable scope. Try : {{ if $pag.HasNext }}

The clue is in the error message. It’s not possible for me to tell where (no source).

thank you, but no luck with that variable scope :frowning_face:

Hi bep, I’m still strugling, kindly check my source https://github.com/yudyananda/mutmut

@bep Not sure if what i’m doing is the best practice, but here is my workaround

add the {{ $.Scratch.Set "type" "index" }} in my layouts/blog/list.html then move {{ $paginator := .Paginate (where .Data.Pages "Type" "blog") 7 }} from the layouts/blog/list.html into partial head `layouts/partials/heads/head.html, where i put the code for checking whether is paginated or not.

here is the complete code from the head
.html

<!-- ROBOTS -->
{{ if $.Scratch.Get "type" }}
{{ $paginator := .Paginate (where .Data.Pages "Type" "blog") 7 }}
 {{ if not $paginator.HasPrev }}
 	<meta name="robots" content="index, follow">
 {{ else }}
    <meta name="robots" content="noindex, follow">
 {{ end }}
 {{ if $paginator.HasPrev }}
    <link rel="prev" href="{{ .Paginator.Prev.URL }}">
 {{ end }}
 {{ if $paginator.HasNext }}
    <link rel="next" href="{{ .Paginator.Next.URL }}">
 {{ end }}
{{ end }}

It works, at least for now, is there better approach?

Hello,

i’m struggling with similar problem.
I have meta partial where I need to find out whether current page is on paged state.
Down, deeper, in next partial I create paginator for some external data.

Is it even possible to find out in this “reversed” order?