Troy
August 31, 2020, 6:34pm
1
All of my pages are sections (highly nested).
I want to show only 9 elements per page.
So I set…
config.toml
paginate = 9
list.html
{{ $paginator := .Paginate .Sections }}
{{ range $paginator.Pages }}
{{ .Title }}
{{ partial "children.html" . }}
{{ end }}
children.html
{{ $child_pages := union .Sections .Pages }}
{{ range $child_pages }}
{{ .Title }}
{{ partial "children.html" . }}
{{ end }}
The problem is that in my setup (see above) the child pages aren’t counted as elements.
So if I have 1 section on level 1 and 20 sections on level 2 all 21 sections are shown.
The problem is the call of {{ partial "children.html" . }}
in my list.html . Everything what happens there seems to be invisible to the paginator. How can I tell the paginator to continue to count?
You need to build a collection of pages, then instantiate a paginator for the collection.
You are currently building a collection of pages, instantiating a paginator, then building another collection of pages.
Please consider sharing your repository if you need additional help. You are more likely to receive timely assistance.
1 Like
Troy
September 1, 2020, 10:36am
3
Thanks @jmooring for the hint. This helped me to find a solution.
list.html
{{ $indexScratch := .Scratch }}
{{ range .Sections }}
{{ $.Scratch.Add "sections" (slice . ) }}
{{ partial "child-pages.html" (dict "indexScratch" $indexScratch "context" .) }}
{{ end }}
{{ $sections := .Scratch.Get "sections" }}
{{ $paginator := .Paginate $sections }}
<div>
{{ range $paginator.Pages }}
<a href="{{.Permalink}}">
{{ .Title }}
</a>
{{ end }}
</div>
{{ partial "pagination.html" . }}
child-pages.html
{{ $indexScratch := .indexScratch }}
{{ $child_pages := union .context.Sections .context.Pages }}
{{ range $child_pages }}
{{ $.indexScratch.Add "sections" (slice . ) }}
{{ partial "lists/child-pages.html" (dict "indexScratch" $indexScratch "context" .) }}
{{ end }}
Similar approach, but passing global context in the dictionary to use scratch in a recursive context.
layouts/_default/list.html
{{- .Scratch.Set "pageCollection" slice -}}
{{- partial "foo.html" (dict "ctx" . "ctxGlobal" $) -}}
{{- range (.Paginate (.Scratch.Get "pageCollection")).Pages -}}
<a href="{{ .RelPermalink }}">{{ .Title }}</a><br>
{{- end -}}
layouts/partials/foo.html
{{- $ctx := .ctx -}}
{{- $ctxGlobal := .ctxGlobal -}}
{{- with $ctx.Sections -}}
{{- range .ByTitle -}}
{{- $ctxGlobal.Scratch.Add "pageCollection" (slice .) -}}
{{- partial "foo.html" (dict "ctx" . "ctxGlobal" $ctxGlobal) -}}
{{- end -}}
{{- end -}}
This content:
content/
├── book
│ ├── book-1
│ │ ├── chapter-1
│ │ │ └── _index.md
│ │ ├── chapter-2
│ │ │ └── _index.md
│ │ ├── chapter-3
│ │ │ └── _index.md
│ │ └── _index.md
│ ├── book-2
│ │ ├── chapter-1
│ │ │ └── _index.md
│ │ ├── chapter-2
│ │ │ └── _index.md
│ │ ├── chapter-3
│ │ │ └── _index.md
│ │ └── _index.md
│ └── _index.md
└── _index.md
Produces:
1 Like
system
Closed
September 3, 2020, 12:24pm
5
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.