Pagination from within Single Template

I have created a basic page content type which will serve multiple templates. The idea is to be able to specify a template for a specific content type otherwise render a standard template (a common requirement).

I am pulling in the content dynamically using the contentful to hugo plugin https://github.com/ArnoNuyts/contentful2hugo. The data (markdown files) is pulled in directly to the hugo/content folder without sub folders. Therefore I am relying on the type value to arrange content.

I am able to iterate over content using the range function however if I try to apply pagination I receive an error:

ERROR: 2016/04/20 template: partials/blog.html:77:19: executing “partials/blog.html” at <.Paginate>: error calling Paginate: Paginators not supported for content pages. in partials/blog.html

Code:

basic_page/single.html:
{{ if isset .Params “template” }}
{{ if in .Params.template.api_object.fields.title “template_blog” }}
{{ partial “blog” . }}
{{ end }}

{{ else }}

{{ .Render "single.content" }}

{{ end }}

blog.html partial

{{ $paginator := .Paginate (where .Site.Pages “Type” “post”) }}
{{ template “_internal/pagination.html” . }}
{{ range $paginator.Pages }}
{{ .Title }}
{{ end }}

{{ range $index, $element := (where .Site.Pages “Type” “post”) }}
{{ if eq $index 0 }}
{{ .Render “summary-first” }}
{{ else }}
{{ .Render “summary” }}
{{ end }}
{{ end }}

The error is pretty clear however can anyone recommend a workaround? The pagination works fine from the homepage however my guess is that by calling it from the single template there is different context and it is failing.

Any recommendations on how I can achieve this? I guess the main issue is that I cannot use sub-folders. The reason I am using a basic page type is so authors can alter the ‘landing pages’ so they can change things like blog header/header image which is rendered on all blog pages.

Remember that this is a static site generator, so all the paginator pages are static, so for the node pages (home page, taxonomy page) there is 0 or 1 paginator (not 2 or 3 … where should we put those pages?).

The content pages currently have no paginator support (hence the error message). I guess they could, but that is a future enhancement. I see several:

  • Once we get proper node support you should maybe be able to do something like paginator = homepage.Paginator from a content page …
  • And (maybe) add paginator support for content pages (1)

Not sure what you are trying to do, but I suspect what you need is something like .NextInSection and .PrevInSection to jump to the next page etc.

1 Like

@bep Thanks for the swift reply. What you’re saying makes perfect sense.

The workaround I have is to place the template in the layouts/index/blog.html and then pagination is working perfectly. However this means I can’t control the page url dynamically through my cms. It sounds like I need to have a directory structure in place the way Hugo intended.