How to check if current page have pagination / is part of pagination?

Is that possible?

// I wan’t to use it in partial CSS file.
// You know - to skip some CSS if there is no pagination

These are not working:
{{ if .Paginate }}
{{ if or ( .Paginate.HasPrev ) ( .Paginate.HasNext ) }}
{{ if .Paginate.TotalPages >= 1 }}

Pagination Documentation

{{ if ( or .Paginator.HasPrev .Paginator.HasNext ) }}

{{ if ge .Paginator.TotalPages 1 }}

1 Like

Thanks for answer.
In my examples I was trying to get “true” value in any way.

What does “ge” mean? I can’t find it in documentation.

I think {{ if ge .Paginator.TotalPages 1 }} should work, but… I’m getting error:

ERROR 2017/06/28 16:19:04 Error while rendering “home”: template: theme/index.html:6:15: executing “theme/index.html” at <.Paginate>: error calling Paginate: a Paginator was previously built for this Node without filters; look for earlier .Paginator usage

In short my index.html looks like this (just an example):

<style>
{{ partial "style.css" . | safeCSS }}
</style>

<main>
    {{ range ( .Paginate (where .Data.Pages "Type" "post")).Pages }}
    <a href="{{ .Permalink }}"><h2>░ {{ .Title }}</h2></a>
    <p>{{ .Params.description }}</p>
    {{ end }}
</main>

<p class="pagination">
    {{- if or (.Paginator.HasPrev) (.Paginator.HasNext) }}
  {{- if .Paginator.HasPrev }}
    <a class="newer" href="{{ .Paginator.Prev.URL }}">NEWER POSTS</a>
  {{- end }}

  {{- if .Paginator.HasNext }}
    <a class="older" href="{{ .Paginator.Next.URL }}">OLDER POSTS</a>
  {{- end }}
{{end}}
</p>

And my style.css looks like (just an example):

{{ if or (eq .Kind "home") (eq .Kind "taxonomy") }}
{{ if ge .Paginator.TotalPages 1 }}
.newer, .older {
	border: 1px solid;
}
{{ end }}
{{ end }}

It seems I need different way to determine if page have pagination.

Go to Hugo Template Functions documentation and search for “Following operators are now available”.

1 Like

Cool, thank you :slight_smile:

Quoting the Pagination documentation:

There are two ways to configure and use a .Paginator:

  1. The simplest way is just to call .Paginator.Pages from a template. It will contain the pages for that page .
  2. Select a sub-set of the pages with the available template functions and ordering options, and pass the slice to .Paginate, e.g. {{ range (.Paginate ( first 50 .Data.Pages.ByTitle )).Pages }}.

For a given Page, it’s one of the options above. The .Paginator is static and cannot change once created.

The problem is that, you are mixing them both. I think what you want is:

{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}
{{ range $paginator.Pages }}
  <a href="{{ .Permalink }}"><h2>{{ .Title }}</h2></a>
  <p>{{ .Params.description }}</p>
{{ end }}

/* ... replace every .Paginator with $paginator ... */
1 Like

One more thing, TotalPages is always going to be at least 1. So, using ge (i.e. >=) will always return true. What you want is gt (i.e. >).

1 Like

You are right.

And about $paginator variable… here is another problem.
It seems it is impossible to access variables from partial files.
When I defined variable in html file, it was undefined in partial css file.
When I defined variable in partial css file, it was undefined in html file.

Do you really the ( where .Data.Pages "Type" "post" ) part?

If not, just use .Paginator. It’s less hassle.

Otherwise, take a look at Scratch documentation.

1 Like

Yes, I need it because otherwise pages like “Contact” or “About” will be listed in paginator.
I want to list only posts from “posts” folder.

There are other ways to solve it.

Anyway, Scratch will help you with variables across partials.

1 Like

That was a bit weird… but I made it to work.
It seems that scratch might be very useful.

Huge thanks for your help! :slight_smile: :slight_smile: :slight_smile: