diff options
author | Joe Mooring <[email protected]> | 2021-06-08 01:41:21 -0700 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-08 10:41:21 +0200 |
commit | 73483d0f9eb46838d41640f88cc05c1d16811dc5 (patch) | |
tree | 0c0bd410f7c4ea98f264076fe5cd3d0e50b3f750 | |
parent | 9b5debe4b820132759cfdf7bff7fe9c1ad0a6bb1 (diff) | |
download | hugo-73483d0f9eb46838d41640f88cc05c1d16811dc5.tar.gz hugo-73483d0f9eb46838d41640f88cc05c1d16811dc5.zip |
tpl: Add a terse pagination template variant to improve performance
These calls are equivalent:
{{ template "_internal/pagination.html" . }}
{{ template "_internal/pagination.html" (dict "page" .) }}
{{ template "_internal/pagination.html" (dict "page" . "format" "default") }}
To use an alternate format:
{{ template "_internal/pagination.html" (dict "page" . "format" "terse") }}
Fixes #8599
-rw-r--r-- | hugolib/embedded_templates_test.go | 39 | ||||
-rw-r--r-- | tpl/tplimpl/embedded/templates.autogen.go | 195 | ||||
-rw-r--r-- | tpl/tplimpl/embedded/templates/pagination.html | 195 |
3 files changed, 343 insertions, 86 deletions
diff --git a/hugolib/embedded_templates_test.go b/hugolib/embedded_templates_test.go index c62d59b1d..5d7d6dc23 100644 --- a/hugolib/embedded_templates_test.go +++ b/hugolib/embedded_templates_test.go @@ -116,3 +116,42 @@ Disqus: // Disqus b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';") } + +func TestEmbeddedPaginationTemplate(t *testing.T) { + t.Parallel() + + test := func(variant string, expectedOutput string) { + b := newTestSitesBuilder(t) + b.WithConfigFile("toml", `paginate = 1`) + b.WithContent( + "s1/p01.md", "---\ntitle: p01\n---", + "s1/p02.md", "---\ntitle: p02\n---", + "s1/p03.md", "---\ntitle: p03\n---", + "s1/p04.md", "---\ntitle: p04\n---", + "s1/p05.md", "---\ntitle: p05\n---", + "s1/p06.md", "---\ntitle: p06\n---", + "s1/p07.md", "---\ntitle: p07\n---", + "s1/p08.md", "---\ntitle: p08\n---", + "s1/p09.md", "---\ntitle: p09\n---", + "s1/p10.md", "---\ntitle: p10\n---", + ) + b.WithTemplates("index.html", `{{ .Paginate (where site.RegularPages "Section" "s1") }}`+variant) + b.Build(BuildCfg{}) + b.AssertFileContent("public/index.html", expectedOutput) + } + + expectedOutputDefaultFormat := "Pager 1\n <ul class=\"pagination pagination-default\">\n <li class=\"page-item disabled\">\n <a href=\"#\" aria-disabled=\"true\" aria-label=\"First\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">««</span></a>\n </li>\n <li class=\"page-item disabled\">\n <a href=\"#\" aria-disabled=\"true\" aria-label=\"Previous\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">«</span></a>\n </li>\n <li class=\"page-item active\">\n <a href=\"#\" aria-current=\"page\" aria-label=\"Page 1\" class=\"page-link\" role=\"button\">1</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Page 2\" class=\"page-link\" role=\"button\">2</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/3/\" aria-label=\"Page 3\" class=\"page-link\" role=\"button\">3</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/4/\" aria-label=\"Page 4\" class=\"page-link\" role=\"button\">4</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/5/\" aria-label=\"Page 5\" class=\"page-link\" role=\"button\">5</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Next\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»</span></a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/10/\" aria-label=\"Last\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»»</span></a>\n </li>\n </ul>" + expectedOutputTerseFormat := "Pager 1\n <ul class=\"pagination pagination-terse\">\n <li class=\"page-item active\">\n <a href=\"#\" aria-current=\"page\" aria-label=\"Page 1\" class=\"page-link\" role=\"button\">1</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Page 2\" class=\"page-link\" role=\"button\">2</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/3/\" aria-label=\"Page 3\" class=\"page-link\" role=\"button\">3</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Next\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»</span></a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/10/\" aria-label=\"Last\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»»</span></a>\n </li>\n </ul>" + + variant := `{{ template "_internal/pagination.html" . }}` + test(variant, expectedOutputDefaultFormat) + + variant = `{{ template "_internal/pagination.html" (dict "page" .) }}` + test(variant, expectedOutputDefaultFormat) + + variant = `{{ template "_internal/pagination.html" (dict "page" . "format" "default") }}` + test(variant, expectedOutputDefaultFormat) + + variant = `{{ template "_internal/pagination.html" (dict "page" . "format" "terse") }}` + test(variant, expectedOutputTerseFormat) +} diff --git a/tpl/tplimpl/embedded/templates.autogen.go b/tpl/tplimpl/embedded/templates.autogen.go index c8732a914..9584077ef 100644 --- a/tpl/tplimpl/embedded/templates.autogen.go +++ b/tpl/tplimpl/embedded/templates.autogen.go @@ -248,51 +248,160 @@ if (!doNotTrack) { {{- /* Facebook Page Admin ID for Domain Insights */}} {{- with .Site.Social.facebook_admin }}<meta property="fb:admins" content="{{ . }}" />{{ end }} `}, - {`pagination.html`, `{{ $pag := $.Paginator }} -{{ if gt $pag.TotalPages 1 -}} -<ul class="pagination"> - {{ with $pag.First -}} - <li class="page-item"> - <a href="{{ .URL }}" class="page-link" aria-label="First"><span aria-hidden="true">««</span></a> - </li> - {{ end -}} - <li class="page-item{{ if not $pag.HasPrev }} disabled{{ end }}"> - <a {{ if $pag.HasPrev }}href="{{ $pag.Prev.URL }}"{{ end }} class="page-link" aria-label="Previous"><span aria-hidden="true">«</span></a> - </li> - {{- $ellipsed := false -}} - {{- $shouldEllipse := false -}} - {{- range $pag.Pagers -}} - {{- $right := sub .TotalPages .PageNumber -}} - {{- $showNumber := or (le .PageNumber 3) (eq $right 0) -}} - {{- $showNumber := or $showNumber (le .TotalPages 5) -}}{{/* Issue #7523 */}} - {{- $showNumber := or $showNumber (and (gt .PageNumber (sub $pag.PageNumber 2)) (lt .PageNumber (add $pag.PageNumber 2))) -}} - {{- if $showNumber -}} - {{- $ellipsed = false -}} - {{- $shouldEllipse = false -}} - {{- else -}} - {{- $shouldEllipse = not $ellipsed -}} - {{- $ellipsed = true -}} - {{- end -}} - {{- if $showNumber }} - <li class="page-item{{ if eq . $pag }} active{{ end }}"> - <a class="page-link" href="{{ .URL }}">{{ .PageNumber }}</a> - </li> - {{- else if $shouldEllipse }} - <li class="page-item disabled"> - <span aria-hidden="true"> … </span> - </li> - {{- end -}} + {`pagination.html`, `{{- $validFormats := slice "default" "terse" }} + +{{- $msg1 := "When passing a map to the internal pagination template, one of the elements must be named 'page', and it must be set to the context of the current page." }} +{{- $msg2 := "The 'format' specified in the map passed to the internal pagination template is invalid. Valid choices are: %s." }} + +{{- $page := . }} +{{- $format := "default" }} + +{{- if reflect.IsMap . }} + {{- with .page }} + {{- $page = . }} + {{- else }} + {{- errorf $msg1 }} {{- end }} - <li class="page-item{{ if not $pag.HasNext }} disabled{{ end }}"> - <a {{ if $pag.HasNext }}href="{{ $pag.Next.URL }}"{{ end }} class="page-link" aria-label="Next"><span aria-hidden="true">»</span></a> - </li> - {{- with $pag.Last }} - <li class="page-item"> - <a href="{{ .URL }}" class="page-link" aria-label="Last"><span aria-hidden="true">»»</span></a> - </li> + {{- with .format }} + {{- $format = lower . }} {{- end }} -</ul> -{{ end }} +{{- end }} + +{{- if in $validFormats $format }} + {{- if gt $page.Paginator.TotalPages 1 }} + <ul class="pagination pagination-{{ $format }}"> + {{- partial (printf "partials/inline/pagination/%s" $format) $page }} + </ul> + {{- end }} +{{- else }} + {{- errorf $msg2 (delimit $validFormats ", ") }} +{{- end -}} + +{{/* Format: default +{{/* --------------------------------------------------------------------- */}} +{{- define "partials/inline/pagination/default" }} + {{- with .Paginator }} + {{- $currentPageNumber := .PageNumber }} + + {{- with .First }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="First" class="page-link" role="button"><span aria-hidden="true">««</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="First" class="page-link" role="button"><span aria-hidden="true">««</span></a> + </li> + {{- end }} + {{- end }} + + {{- with .Prev }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">«</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">«</span></a> + </li> + {{- end }} + + {{- $slots := 5 }} + {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} + {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} + {{- if lt (add (sub $end $start) 1) $slots }} + {{- $start = math.Max 1 (add (sub $end $slots) 1) }} + {{- end }} + + {{- range $k := seq $start $end }} + {{- if eq $.Paginator.PageNumber $k }} + <li class="page-item active"> + <a href="#" aria-current="page" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- else }} + <li class="page-item"> + <a href="{{ (index $.Paginator.Pagers (sub $k 1)).URL }}" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- end }} + {{- end }} + + {{- with .Next }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">»</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">»</span></a> + </li> + {{- end }} + + {{- with .Last }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">»»</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">»»</span></a> + </li> + {{- end }} + {{- end }} + {{- end }} +{{- end -}} + +{{/* Format: terse +{{/* --------------------------------------------------------------------- */}} +{{- define "partials/inline/pagination/terse" }} + {{- with .Paginator }} + {{- $currentPageNumber := .PageNumber }} + + {{- with .First }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="First" class="page-link" role="button"><span aria-hidden="true">««</span></a> + </li> + {{- end }} + {{- end }} + + {{- with .Prev }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">«</span></a> + </li> + {{- end }} + + {{- $slots := 3 }} + {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} + {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} + {{- if lt (add (sub $end $start) 1) $slots }} + {{- $start = math.Max 1 (add (sub $end $slots) 1) }} + {{- end }} + + {{- range $k := seq $start $end }} + {{- if eq $.Paginator.PageNumber $k }} + <li class="page-item active"> + <a href="#" aria-current="page" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- else }} + <li class="page-item"> + <a href="{{ (index $.Paginator.Pagers (sub $k 1)).URL }}" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- end }} + {{- end }} + + {{- with .Next }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">»</span></a> + </li> + {{- end }} + + {{- with .Last }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">»»</span></a> + </li> + {{- end }} + {{- end }} + {{- end }} +{{- end -}} `}, {`schema.html`, `<meta itemprop="name" content="{{ .Title }}"> <meta itemprop="description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}"> diff --git a/tpl/tplimpl/embedded/templates/pagination.html b/tpl/tplimpl/embedded/templates/pagination.html index 0984b6f9e..b3535ffca 100644 --- a/tpl/tplimpl/embedded/templates/pagination.html +++ b/tpl/tplimpl/embedded/templates/pagination.html @@ -1,45 +1,154 @@ -{{ $pag := $.Paginator }} -{{ if gt $pag.TotalPages 1 -}} -<ul class="pagination"> - {{ with $pag.First -}} - <li class="page-item"> - <a href="{{ .URL }}" class="page-link" aria-label="First"><span aria-hidden="true">««</span></a> - </li> - {{ end -}} - <li class="page-item{{ if not $pag.HasPrev }} disabled{{ end }}"> - <a {{ if $pag.HasPrev }}href="{{ $pag.Prev.URL }}"{{ end }} class="page-link" aria-label="Previous"><span aria-hidden="true">«</span></a> - </li> - {{- $ellipsed := false -}} - {{- $shouldEllipse := false -}} - {{- range $pag.Pagers -}} - {{- $right := sub .TotalPages .PageNumber -}} - {{- $showNumber := or (le .PageNumber 3) (eq $right 0) -}} - {{- $showNumber := or $showNumber (le .TotalPages 5) -}}{{/* Issue #7523 */}} - {{- $showNumber := or $showNumber (and (gt .PageNumber (sub $pag.PageNumber 2)) (lt .PageNumber (add $pag.PageNumber 2))) -}} - {{- if $showNumber -}} - {{- $ellipsed = false -}} - {{- $shouldEllipse = false -}} - {{- else -}} - {{- $shouldEllipse = not $ellipsed -}} - {{- $ellipsed = true -}} - {{- end -}} - {{- if $showNumber }} - <li class="page-item{{ if eq . $pag }} active{{ end }}"> - <a class="page-link" href="{{ .URL }}">{{ .PageNumber }}</a> - </li> - {{- else if $shouldEllipse }} - <li class="page-item disabled"> - <span aria-hidden="true"> … </span> - </li> - {{- end -}} +{{- $validFormats := slice "default" "terse" }} + +{{- $msg1 := "When passing a map to the internal pagination template, one of the elements must be named 'page', and it must be set to the context of the current page." }} +{{- $msg2 := "The 'format' specified in the map passed to the internal pagination template is invalid. Valid choices are: %s." }} + +{{- $page := . }} +{{- $format := "default" }} + +{{- if reflect.IsMap . }} + {{- with .page }} + {{- $page = . }} + {{- else }} + {{- errorf $msg1 }} {{- end }} - <li class="page-item{{ if not $pag.HasNext }} disabled{{ end }}"> - <a {{ if $pag.HasNext }}href="{{ $pag.Next.URL }}"{{ end }} class="page-link" aria-label="Next"><span aria-hidden="true">»</span></a> - </li> - {{- with $pag.Last }} - <li class="page-item"> - <a href="{{ .URL }}" class="page-link" aria-label="Last"><span aria-hidden="true">»»</span></a> - </li> + {{- with .format }} + {{- $format = lower . }} {{- end }} -</ul> -{{ end }} +{{- end }} + +{{- if in $validFormats $format }} + {{- if gt $page.Paginator.TotalPages 1 }} + <ul class="pagination pagination-{{ $format }}"> + {{- partial (printf "partials/inline/pagination/%s" $format) $page }} + </ul> + {{- end }} +{{- else }} + {{- errorf $msg2 (delimit $validFormats ", ") }} +{{- end -}} + +{{/* Format: default +{{/* --------------------------------------------------------------------- */}} +{{- define "partials/inline/pagination/default" }} + {{- with .Paginator }} + {{- $currentPageNumber := .PageNumber }} + + {{- with .First }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="First" class="page-link" role="button"><span aria-hidden="true">««</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="First" class="page-link" role="button"><span aria-hidden="true">««</span></a> + </li> + {{- end }} + {{- end }} + + {{- with .Prev }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">«</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">«</span></a> + </li> + {{- end }} + + {{- $slots := 5 }} + {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} + {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} + {{- if lt (add (sub $end $start) 1) $slots }} + {{- $start = math.Max 1 (add (sub $end $slots) 1) }} + {{- end }} + + {{- range $k := seq $start $end }} + {{- if eq $.Paginator.PageNumber $k }} + <li class="page-item active"> + <a href="#" aria-current="page" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- else }} + <li class="page-item"> + <a href="{{ (index $.Paginator.Pagers (sub $k 1)).URL }}" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- end }} + {{- end }} + + {{- with .Next }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">»</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">»</span></a> + </li> + {{- end }} + + {{- with .Last }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">»»</span></a> + </li> + {{- else }} + <li class="page-item disabled"> + <a href="#" aria-disabled="true" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">»»</span></a> + </li> + {{- end }} + {{- end }} + {{- end }} +{{- end -}} + +{{/* Format: terse +{{/* --------------------------------------------------------------------- */}} +{{- define "partials/inline/pagination/terse" }} + {{- with .Paginator }} + {{- $currentPageNumber := .PageNumber }} + + {{- with .First }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="First" class="page-link" role="button"><span aria-hidden="true">««</span></a> + </li> + {{- end }} + {{- end }} + + {{- with .Prev }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Previous" class="page-link" role="button"><span aria-hidden="true">«</span></a> + </li> + {{- end }} + + {{- $slots := 3 }} + {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} + {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} + {{- if lt (add (sub $end $start) 1) $slots }} + {{- $start = math.Max 1 (add (sub $end $slots) 1) }} + {{- end }} + + {{- range $k := seq $start $end }} + {{- if eq $.Paginator.PageNumber $k }} + <li class="page-item active"> + <a href="#" aria-current="page" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- else }} + <li class="page-item"> + <a href="{{ (index $.Paginator.Pagers (sub $k 1)).URL }}" aria-label="Page {{ $k }}" class="page-link" role="button">{{ $k }}</a> + </li> + {{- end }} + {{- end }} + + {{- with .Next }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Next" class="page-link" role="button"><span aria-hidden="true">»</span></a> + </li> + {{- end }} + + {{- with .Last }} + {{- if ne $currentPageNumber .PageNumber }} + <li class="page-item"> + <a href="{{ .URL }}" aria-label="Last" class="page-link" role="button"><span aria-hidden="true">»»</span></a> + </li> + {{- end }} + {{- end }} + {{- end }} +{{- end -}} |