Title for paginated pages?

Hello.

I am setting <title> in my layouts/partials/metadata.html and calling that partial in my layouts/_default/baseof.html. I’m using conditions to set the title:

{{ .Scratch.Set "currentPage" .Permalink}}

{{ if .IsHome }}

    {{ .Scratch.Set "title" .Site.Params.Title }}

    {{ else if in (.Scratch.Get "currentPage") "tag" }}

        {{ .Scratch.Set "title" (printf "Projects tagged %s | %s" .Title .Site.Params.Title) }}

        {{ else if in (.Scratch.Get "currentPage") "project" }}

            {{ .Scratch.Set "title" (printf "%s | %s" .Title .Site.Params.Title) }}

{{ end }}

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

It’s working fine. However, I was wondering if I could change the title for paginated pages on the home as well as tag pages. I was thinking of titles like Page 1 | Projects tagged .... But, the .Permalink doesn’t include URL like /tag/ABC/page/2. It’ll just show the URL as /tag/ABC/.

Is there any other way to set different title for paginated pages?

This is probably a resources eater, but you could run and scratch the pagination once in your title template. You could run it like the navigation sample here:

By the way, off topic: HTML requires you to have a title tag with content. Your code above will result in an emtpy title tag if it’s not the home page, a tag page or a project page. You could rewrite it like this to show the site title as fallback if nothing else fits:

{{ .Scratch.Set "currentPage" .Permalink}}
{{ .Scratch.Set "title" .Site.Params.Title }}
{{ if in (.Scratch.Get "currentPage") "tag" }}
    {{ .Scratch.Set "title" (printf "Projects tagged %s | %s" .Title .Site.Params.Title) }}
{{ else if in (.Scratch.Get "currentPage") "project" }}
    {{ .Scratch.Set "title" (printf "%s | %s" .Title .Site.Params.Title) }}
{{ end }}
<title>{{ .Scratch.Get "title" }}</title>

If it’s not too much work, would it be possible for you to give a little example, as I didn’t really understand what you said?

Yeah, I had trimmed the last else statement as it wasn’t required in the discussion. But, thanks for the tip.

Something along these lines:

{{ $theTitle := .Title }}
{{ $titlePaginator := .Paginate (where .Pages "Type" "posts") }}
{{ template "_internal/pagination.html" . }}
{{ range $titlePaginator.Pages }}
   {{ if (eq $theTitle .Title) }}
   		{{ $currentPageNumber := .PageNumber }}
   		{{ $totalPageNumber := .TotalPages }}
	{{ end }}
{{ end }}
<title>{{ $yourtitle }} / Page {{ $currentPageNumber }} of {{ $totalPageNumbers }}</title>

This probably has to come within those if/else constructs so you can configure the {{ $titlePaginator-kine according to the page type you will paginate in the end.

Untested. Also: I am not sure if that will mess up other paginators on the page. There are issues with multiple paginators and I don’t know right now if that is solved by having a nice custom variable for this run.

1 Like

Thanks a lot! That works perfectly.

I modified it like this:

{{ $indexPaginator := .Paginate (where .Site.RegularPages "Type" "in" .Site.Params.mainSections) }}

    {{ range $indexPaginator.Pages }}

        {{ $.Scratch.Set "title" (printf "Page %v of %v | %s" $indexPaginator.PageNumber $indexPaginator.TotalPages .Site.Params.Title) }}
		
	{{ end }}

This line was rendering the page numbers on the page, so, I removed it. I guess, it’s meant to render the pages, so removing that was necessary.

One question though, is it possible to only paginate the tags now?

Like, I’m paginating the tags in my layouts/_default/list.html like this {{ range .Paginator.Pages }} simple as that. But, can I define in my metadata layout to paginate per tag?

I just tried this: {{ $tagPaginator := .Paginate (where .Site.RegularPages "Type" "in" .Site.Taxonomies.tags) }}. It renders nothing on my list pages.

Haha, I should have tried that same thing before asking you. Here’s what I’m doing now:

...if statement starts before

{{ else if in (.Scratch.Get "currentPage") "tag" }}

    {{ range $tagName, $taxonomy := .Site.Taxonomies.tags }}

        {{ range $.Paginator.Pages }}

            {{ $.Scratch.Set "title" (printf "Page %v of %v | Projects tagged %s | %s" $.Paginator.PageNumber $.Paginator.TotalPages $tagName .Site.Params.Title) }}

        {{ end }}
		
    {{ end }}

... more code below

The only problem now is that, I can’t figure out how to give the tag name to the page title here: Projects tagged %s. I want that %s to get replaced with the tag name. Before I converted to this pagination-based page title, I was simply using .Title and it was giving me the correct tag name. However, now it’s not. Now it gives the name of the last tag (from the list of all my website tags, ordered alphabetically).

So, yeah, last question for this topic, how to get the tag name in there? Thanks a lot.

UPDATE:

Solved it:

{{ $currentTag := .Title }}

{{ range .Site.Taxonomies.tags }}

    {{ range $.Paginator.Pages }}

        {{ $.Scratch.Set "title" (printf "Page %v of %v | Projects tagged %s | %s" $.Paginator.PageNumber $.Paginator.TotalPages $currentTag .Site.Params.Title) }}

    {{ end }}
			
{{ end }}

Before entering the range, I set the value of a variable that would give me the title and then used it. This works fine. Thanks a lot for the help @davidsneighbour.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.