Access paginator from header

I`m trying to make rel links in <head:
<lnk rel=“prev” href=“part1.html”
<lnk rel=“next” href=“part3.html”

but when i try to access .Paginator i get error

ERROR 2018/05/24 12:37:41 Error while rendering “home” in “”: template: C:\Hugo\www.example.com\themes\theme\layouts_default\list.html:6:33: executing “main” at <.Paginate>: error calling Paginate: a Paginator was previously built for this Node without filters; look for earlier .Paginator usage

I`m new to Hugo, an would appreciate any help

I think you can check with .HasPrev and .HasNext to see if a next or previous pagination page is available.

For your case, that would look like this I think:

{{ if .Paginator.HasPrev }}
    <link rel="prev" href="{{ .Paginator.Prev.URL }}">
{{ end }}

{{ if .Paginator.HasNext }}
    <link rel="next" href="{{ .Paginator.Next.URL }}">
{{ end }}

as i told befote when i try to access .Paginator from header i get error
i think thats because i`m using custom pagination further in code, but i need it too )

This is a little hard to explain in few words:

  • But the paginator is static, so it is created the first time you access it (with .Paginate or .Paginator)
  • You are accessing it in the header then changing it later, this does not work

I suggest you look into base templates and blocks and make sure it is created with the params you want before you access it.

Well, it`s obvious. But i was hoping to see some simple examples how to handle it :slight_smile:

No thoughts? This links important for SEO, so I will be grateful for any help

Here’s my workaround

This is part of the <head> for my index page

<!-- ROBOTS POSTS INDEX -->
{{ if $.Scratch.Get "blogs" }}
{{ $paginator := .Paginate (where .Data.Pages "Type" "article") 6 }}
	{{ 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 }}

this is part where the list.html or pagination part

<!-- I move this part into my head
{{ $paginator := .Paginate (where .Data.Pages "Type" "article") 6 }}
-->
{{ range $paginator.Pages }} 
 ....your index here

As you can see I move the part of paginator {{ $paginator := .Paginate (where.... to the head

Hope this help

That`s helped a lot, but there was few errors, which i already fixed.
First one resolved by adding “$paginator := .Paginate …” func one more time in list.html
Second one is about Paginator doesn’t work for Kind page

So the final code is:

head-meta.html

{{if eq .Kind "taxonomy"}}
    {{ $paginator := .Paginate (where .Data.Pages "Type" "skin") (index .Site.Params "paginate" | default 10) }}
    {{ if $paginator.HasPrev }}
        <link rel="prev" href="{{ .Paginator.Prev.URL }}" />
    {{ end }}
    {{ if $paginator.HasNext }}
        <link rel="next" href="{{ .Paginator.Next.URL }}" />
    {{ end }}
{{ end }}

list.html

{{ $paginator := .Paginate (where .Data.Pages "Type" "skin") (index .Site.Params "paginate" | default 10) }}
{{ range $paginator.Pages }}
    {{ .Render "content" }}
{{ end }}

Thanks a lot everyone