Pagination logic problem

Hi folks,

On my blog I list the first 6 items in my archive listing, with the first one slightly differently formatted to kind of feature it. The problem is my pagination logic is flawed somewhere, because when I go to subsequent list pages using the arrows/numbers in the pagination footer, it skips listing the post inbetween the last on the previous page and the first on the current page.

Another way to put this is that I want to style the first item on the first blog page differently, since it’s the latest one without having to set any special front matter.

Hope that makes sense. I think I’m misunderstanding how paginator ranges work perhaps?

Here’s the code from the blog template. In my config, paginate is set to 7.

<div class="content">
{{if eq .Paginator.PageNumber 1}}
 {{ range first 1 .Pages.ByPublishDate.Reverse }}
 <span class="date">{{ .Date.Format (.Site.Params.dateFormat | default "January 2, 2006" ) }}</span>
<h1 class="title"><a href="{{.Permalink}}">{{.Title}}</a></h1>

<p>{{.Summary }}</p>
  {{ if .Truncated }}
  <div>
<a href="{{ .RelPermalink }}">Read More…</a>
  </div>
  {{ end }} 
  
{{ end }}
{{end}}
</div>


{{ range (after 1 .Paginator.Pages) }}
   <span class="date">{{ .Date.Format (.Site.Params.dateFormat | default "January 2, 2006" ) }}</span>
{{ .Render "summary"}}
{{ end }}
   {{ template "_internal/pagination.html" . }}
   
  </div>

The first one selects from a different collection than the other one. Also, the sorting should not be needed, as pubDate desc is near the top in the default sort.

Thanks. My thinking behind this was because I only want the first post of the first paginated page to be different, not subsequent pages. Have I completely got this confused?

So if I do this, I get nearly the desired behaviour, but a repeat of the first post listed. How can I skip the first post in this list without doing it for subsequent paginations?

{{ range ( .Paginator.Pages) }}
   <span class="date">{{ .Date.Format (.Site.Params.dateFormat | default "January 2, 2006" ) }}</span>
    {{ .Render "summary"}}
    {{end}}

So,

.Pages.ByPublishDate.Reverse =/= .Paginator.Pages

.Pages.ByPublishDate.Reverse = [A1, A2, A3, ...]
.Paginator.Pages = [B1, B2, B3 ...]

first 1 .Pages.ByPublishDate.Reverse ==> [A1]
after 1 .Paginator.Pages ==> [B2, B3 ...]

You can do something like this instead:

{{ $firstpage := eq .Paginator.PageNumber 1}}
{{ range $i, $e := .Paginator.Pages }}
   {{ if and $firstpage (eq 0 $i) }} <!-- Page 1 AND first .Page -->
     first 
   {{ else }}
     not first
   {{ end }}
{{ end }}
1 Like

Thanks. I’m struggling with this a bit as it’s starting to move beyond my Go syntax skills. That stray bracket the second line after Pages – is that whole $e… part meant to be in brackets or was that end one just a typo?

Ah, typo. Fixed it.

Edited to add:

This is what that line is doing: {{ range $i, $e := ... }} : docs

Ah, getting there. Arrays start at 0 in Go land, yes? So this gives me the first one:

{{ if and $firstpage (eq 0 $i) }}

Yep! fixed, again. Apologies, my brain has already started the weekend :sweat_smile:

That’s okay, I appreciate the help. I’ve sort of got it working, but now I’ve messed up how the divs render. Two steps forward, one step back…

Working now. Thanks for your help. In case anyone else wants it, here’s the entire template code:

{{ define "header_css" }}{{ end }}
{{ define "body_classes" }}page-default{{ end }}
{{ define "header_classes" }}{{ end }}

{{ define "main" }}


<div class="strip">
	<div class="container pt-4 pb-16">
		<div class="row">
			<div class="col-12">
				<h1 class="title">
					{{ .Title | default .Site.Title }}
				</h1>
				{{ $firstpage := eq .Paginator.PageNumber 1}} {{ range $i, $e := .Paginator.Pages }} {{ if and $firstpage (eq 0 $i) }} 
<!-- Page 1 AND first .Page -->
				<div class="content">
					<span class="date">
						{{ .Date.Format (.Site.Params.dateFormat | default "January 2, 2006" ) }}
					</span>
					<h1 class="title">
						<a href="{{.Permalink}}">
							{{.Title}}
						</a>
					</h1>
					<p>
						{{ .Content }}
					</p>
				</div>
				{{ else }} 
				<div class="publications">
					<span class="date">
						{{ .Date.Format (.Site.Params.dateFormat | default "January 2, 2006" ) }}
					</span>
					{{ .Render "summary"}} {{ end }} {{ end }} 
				</div>
				{{ template "_internal/pagination.html" . }} 
			</div>
		</div>
	</div>
</div>


{{ end }}

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