Filtered pagination results in gaps

This is the same issue as was discussed in this post.

Among many articles, some have a param in front matter such as exclude_flags: ["something", "or something else"].

I’ve been unable to filter articles with this param from paginated results without getting gaps, as described in the previous post about this. One of my many attempts to do so is below, but this still results in the undesired content items appearing in the paginated results.

{{ $allPages := .Data.Pages }}
{{ $exclude_pages := where $allPages ".Params.exclude_flags" "intersect" (slice "") }}
{{ $include_pages := symdiff $allPages $exclude_pages }}
{{ $paginator := .Paginate $include_pages }}
{{ range $paginator.Pages }}
    {{ partial "article/card.html" . }}
{{ end }}

Is there some established solution to this problem?

Which articles are you trying to exclude from the list?

When you don’t have any exclude flags, do you have any of these in front matter?

exclude_flags = ""
exclude_flags = [""]
exclude_flags = []

Are you using .Paginator or .Paginate anywhere else in this template, or up the template chain?

No, when I don’t have any exclude flags exclude_flags does not appear in front matter, but I wanted to account for that in case it happens in the future.

I have also tried the following without luck (and many other variations):

{{ $allPages := .Data.Pages }}
{{ $include_pages := where $allPages "Params.exclude_flags" "intersect" "" }}
{{ $include_pages = where $non_essay_contest_pages "not" "Params.exclude_flags" "isset" }}

{{ $paginator := .Paginate $include_pages }}
{{ range $paginator.Pages }}
  {{ partial "article/card.html" . }}
{{ end }}

I’m not using .Paginator or .Paginate anywhere else in this template or up the template chain.

{{ range where .Pages "Params.exclude_pages" nil }}

That gives you all the pages where exclude_pages isn’t set.

1 Like

Yes, that indeed works perfectly–without using .Paginate. For example, the following fails to filter out articles with an exclude flag from paginated results:

{{ $include_pages := where .Pages "Params.exclude_flags" nil }}
{{ $paginator := .Paginate $include_pages}}
{{ range $paginator.Pages }}
  {{ partial "article/card.html" . }}
{{ end }}

Pagination is irrelevant. You have a different problem. You’ll need to share your project repository, privately if you wish.

1 Like

Run this in the root of your project:

$ grep  -r --include *.html .Paginat

You’ll see the stuff you already know about, plus the stuff you don’t know about:

node_modules/@hyas/seo/layouts/partials/hugo-seo/private/params/general.html:  {{$.s.SetInMap "params" "canonical" (.Paginator.URL | absURL) }}
node_modules/@hyas/seo/layouts/partials/hugo-seo/private/params/general.html:    {{ if .Paginator.HasPrev -}}
node_modules/@hyas/seo/layouts/partials/hugo-seo/private/params/general.html:      {{$.s.SetInMap "params" "prev" (.Paginator.Prev.URL | absURL) }}
node_modules/@hyas/seo/layouts/partials/hugo-seo/private/params/general.html:    {{ if .Paginator.HasNext -}}
node_modules/@hyas/seo/layouts/partials/hugo-seo/private/params/general.html:      {{$.s.SetInMap "params" "next" (.Paginator.Next.URL | absURL) }}

And here’s the culprit.

https://github.com/gethyas/seo/blob/main/layouts/partials/hugo-seo/private/params/general.html#L109-L116

See documentation:

https://gohugo.io/templates/pagination/#list-paginator-pages

If you call .Paginator or .Paginate multiple times on the same page, you should ensure all the calls are identical. Once either .Paginator or .Paginate is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.

1 Like

Thank your for finding this!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.