From 9add06230034d894e32738157152da723bb313d8 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sun, 25 Jan 2026 17:54:13 +0100 Subject: [PATCH] refactor: centralize pagination logic (#1227) * refactor: centralize pagination logic into a new helper partial used for page titles and index display * fix: list.html does not exclude hidden pages * refactor: Simplify title generation logic --- layouts/_default/list.html | 15 ++++---- layouts/index.html | 6 ++-- layouts/partials/data/title.html | 47 ++++++++------------------ layouts/partials/helper/paginator.html | 23 +++++++++++++ 4 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 layouts/partials/helper/paginator.html diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 9bc618d..d2ed9ea 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -46,6 +46,7 @@ {{- $subsections := .Sections -}} {{- $pages := .Pages | complement $subsections -}} + {{- $pages = where $pages "Params.hidden" "!=" true -}} {{- if eq (len $pages) 0 -}} {{/* If there are no normal pages, display subsections in list style, with pagination */}} @@ -68,12 +69,14 @@ {{- end -}} {{/* List only pages that are not a subsection */}} - {{ $paginator := .Paginate $pages }} -
- {{ range $paginator.Pages }} - {{ partial "article-list/compact" . }} - {{ end }} -
+ {{ $paginator := partial "helper/paginator.html" . }} + {{ with $paginator }} +
+ {{ range .Pages }} + {{ partial "article-list/compact" . }} + {{ end }} +
+ {{ end }} {{- partial "pagination.html" . -}} diff --git a/layouts/index.html b/layouts/index.html index 189daa6..d528ac1 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -1,10 +1,8 @@ {{ define "main" }} - {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} - {{ $filtered := where $pages "Params.hidden" "!=" true }} - {{ $pag := .Paginate ($filtered) }} + {{- $paginator := partial "helper/paginator.html" . -}}
- {{ range $index, $element := $pag.Pages }} + {{ range $index, $element := $paginator.Pages }} {{ partial "article-list/default" . }} {{ end }}
diff --git a/layouts/partials/data/title.html b/layouts/partials/data/title.html index 7faa3fb..cc4d4e8 100644 --- a/layouts/partials/data/title.html +++ b/layouts/partials/data/title.html @@ -1,37 +1,20 @@ -{{- $title := .Title -}} +{{- $pageTitle := .Title -}} {{- $siteTitle := .Site.Title -}} +{{- $paginator := partial "helper/paginator.html" . -}} -{{- if .IsHome -}} - +{{- $title := slice -}} - - {{ $pages := where .Site.RegularPages "Section" "in" .Site.Params.mainSections }} - {{ $filtered := where $pages "Params.hidden" "!=" true }} - {{ $pag := .Paginate ($filtered) }} - - {{ if .Paginator.HasPrev }} - - {{ $title = printf "%s - %s" .Paginator $siteTitle }} - {{ else }} - {{ $title = $siteTitle}} - {{ end }} -{{- else if eq .Kind "term" -}} - - - - {{ $notHidden := where .Pages "Params.hidden" "!=" true }} - {{ $pag := .Paginate ($notHidden) }} - - - {{ $title = slice (title .Data.Singular) ": " $title }} - - {{ if .Paginator.HasPrev }} - - {{ $title = $title | append " - " .Paginator }} - {{ end }} - - {{ $title = $title | append " - " $siteTitle }} - {{ $title = delimit $title "" }} +{{- if $pageTitle }} + {{- $title = slice $pageTitle -}} {{- end -}} -{{ return $title }} \ No newline at end of file +{{- if and $paginator $paginator.HasPrev -}} + + {{ $title = $title | append (printf "Page %d" $paginator.PageNumber) }} +{{- end -}} + +{{- if not .IsPage -}} + {{ $title = $title | append $siteTitle }} +{{- end -}} + +{{ return delimit $title " - " }} \ No newline at end of file diff --git a/layouts/partials/helper/paginator.html b/layouts/partials/helper/paginator.html new file mode 100644 index 0000000..55f6a66 --- /dev/null +++ b/layouts/partials/helper/paginator.html @@ -0,0 +1,23 @@ +{{- $pages := "" -}} + +{{- if .IsHome -}} + {{ $pages = where .Site.RegularPages "Section" "in" .Site.Params.mainSections }} + {{ $pages = where $pages "Params.hidden" "!=" true }} +{{- else if eq .Kind "term" -}} + {{ $pages = where .Pages "Params.hidden" "!=" true }} +{{- else if or (eq .Kind "section") (eq .Kind "taxonomy") -}} + {{ $subsections := .Sections }} + {{ $pages = .Pages | complement $subsections }} + {{ $pages = where $pages "Params.hidden" "!=" true }} + + {{- if eq (len $pages) 0 -}} + {{/* See complete logic in list.html */}} + {{- $pages = $subsections -}} + {{- end -}} +{{- end -}} + +{{- if $pages -}} + {{ $pages = .Paginate $pages }} +{{- end -}} + +{{ return $pages }}