Attempt to append Paginator data in Site Title breaks the Paginator

Before publishing the changes I make, I run Xenu over Hugo’s Server URL to check if I didn’t leave behind anything broken.

Today I noticed A LOT of pages had the same title. Some of them it’s a little out of my control because they’re untranslatable names but others, like pages of listing results, could be worked to add Paginator’s data and get rid of this SEO issue.

Then I’ve coded the following routine in my partials/headers.html, prepended in all other templates:

{{ $.Scratch.Set "title" "" }}

{{ if ( and (ne .Title "") (not .IsHome) (ne .Kind "post") ) }}
    {{ $.Scratch.Set "title" (.Title | plainify) }}
{{ end }}

<!-- Appending Paginator Data, if needed -->

{{ if (ne .Kind "page") }}

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

    {{ $prefix := (i18n "paginator-page-prefix" | default "__MISSING__") }}
    {{ $of     := (i18n "paginator-page-connector" | default "__MISSING__") }}

    {{ $c      := (.Paginator.PageNumber) }}
    {{ $t      := (.Paginator.TotalPages) }}

    {{ $.Scratch.Set "title" (printf "%s | %s %d %s %d" ($.Scratch.Get "title") ($prefix) ($c) ($of) ($t) ) }}

  {{ end }}

{{ end }}

<!-- Appending Site Name -->

{{ $i18n := i18n "title-site-name-full" | default "__MISSING__" }}

{{ $.Scratch.Set "title" (printf "%s | %s" ($.Scratch.Get "title") ($i18n) ) }}

<title>{{ $.Scratch.Get "title" }}</title>

And after re-syncing, the titles of my listing results pages had Page Title | Page X of Y | Site Name.

It would be perfect, but that broke the Paginator itself.

In my _default;list.html I have nothing that could be causing this issue, or at least I think:

{{ $items := (.Paginator 6).Pages }}

{{ range $offset, $current := $items }}
    // Lots of logic and markups to build different types of paginations
{{ end }}

{{ if or .Paginator.HasPrev .Paginator.HasNext }}
    // Pagination markup
{{ end }}

Without the modification I made to build a richer and more SEO-friendly <title> all Paginations work perfectly showing a maximum of 6 items per page, but adding that code I have 10.

I didn’t set 10 anywhere of my code or in my config.toml prior to this issue, but I assume that there’s a hidden, forced default value because when I manually set paginate = 6 everything worked as intended.

Still, I would like to know if I can do this in template-time because otherwise, it’s of no use putting the 6over there.

It is a tall order expecting anyone to read your long post with those copy and paste snippets and expect anyone to understand what is going on.

I have no clue what you mean by “breaks the paginator” etc.

I think you need to post a link to the full failing source, and someone might have a look at it.

But I would suggest creating something simpler. Simpler is almost always the best. Easier to understand, too.

First, I’m not requesting anything. You would know that if you read the “long post” — which is only “long” because of code fragments, other than that is just 15 lines of text.

Second, without copying and pasting nobody would be able to discuss programming issues. Of course, I copied and paste the second block, I haven’t developed telepathy yet, but you couldn’t tell if I took it from my code or from the docs/sample theme because I’ve suppressed everything that makes my theme unique. Even a simple {{ . }} within that {{ range }} would be enough to have a working code.

And what’s the purpose of a programming discussion board, themed or not, if not stop by, read some code and try to help? If that’s too much of a burden, well, then move along, there are zillions of websites to visit instead of the one you don’t have any interest.

Third, kind of going back to the first but, anyway… the issue has been already solved (is highlighting easier to notice?) but, in my innocence, I wanted to show that there’s a problem with the Paginator falling back to a Site Configuration value even if that hasn’t been defined.

Fine, the topic category might not be the most adequate, but it’s a minor and quickly editable issue caused by cached information of this odd editor from the last time I thought about asking something here… before I remembered anything posted here doesn’t receive the bare minimum attention or respect.

Anyway, if anyone else is not super tired to read, feel free to use this snippet (the first one) to have more SEO-friendly website/blog. Just be aware of this hidden caveat.

Not sure what you mean by the above. I’ve looked into your topics history and whenever you got a reply by me or others it was quite respectful.

But you know it’s not like we provide professional help in this forum. And to get a reply it depends on whether we have the time to answer and whether the help request is interesting enough.

I read it and I did not understand it. So I suggested you post a link to the source.

When you call .Paginator.HasPrev in the header, you’re creating a paginator with default values; calling .Paginator 6 in the body doesn’t re-create it with new values.


The .Paginator is static and cannot change once created.


1 Like