Paginate Sections?

I have a section of my site with subsections. Each subsection has a content inside:

content/
├── teaching/
│   ├── _index.md
│   ├── lessonOne/
│   │   ├── _index.md
│   │   ├── notes.md
│   │   ├── slides.html
│   ├── lessonTwo/
│   │   ├── _index.md
│   │   ├── slides.html

I want to create a template for /teaching/_index.md that paginates over each section, allowing me to render a paginated list of the sections and their subsections. How do I range over the sections? My first thought is something like:

{{ if .Sections }}
{{ range (.Paginator 3) .Sections }}
 <!-- Content for section listing -->
{{ end }}
{{ end }}

But that’s giving me an error: range can't iterate over Pager

link to repo: https://github.com/KeishaSPerkins/ksp-landing-0918.git

Hi,

If I understand correctly what you are trying to do, you want to render multiple paginators on the same page? I’m not sure you can do that.

From Pagination | Hugo :

The .Paginator is static and cannot change once created.

@pointyfar

If I understand correctly what you are trying to do, you want to render multiple paginators on the same page?

No, I’d like one paginator that is iterating over the sections. The code above works when I try to iterate over pages, but not sections.

Ok, thanks for explaining.

{{ $ps := .Paginate .Sections }}
{{ range $ps.Pages }}
    {{ .Title }}
{{ end }}

You could try the above inside your if .Sections block.

Note: You may need to test with {{ $ps := .Paginate .Sections 2 }} to force it to paginate since you only have 3 Sections. Or add more sections.

3 Likes

@pointyfar Thanks! That worked!