Filtered Pagination results in a "gap"

This is a long topic and I’ll provide different information to fully describe a scenario. Please be patient and join if you really have something to say.

For years now I’ve used the most basic Pagination I’ve learned:

{{ range $offset, (.Paginator).Pages }}
  Do something
{{ end }}

And it always worked. But now that I finally managed to create componentized Content Files with an “include” Shortcode I needed a way to filter out these pluggable pages that can be individually accessed, with their own URLs.

So I’ve added a “boolean” FrontMatter Parameter listed in order to condition the range:

{{ range $offset, (where (.Paginator).Pages ".Params.listed" "!=" "false") }}
    Do something
{{ end }}

If the said Parameter doesn’t exist, as far as I could notice, it’s automatically considered a true in the condition and it’s normally processed by the range. But if it is set -AND- defined as false, it’s filtered out.

This works as intended but produces an unwanted behaviour.

My Paginator is defined to have only 6 elements per page. Pages in which all elements are marked to be listed (i.e. do not have the Parameter), all 6 markups are perfectly rendered. But in pages in which one or more elements are marked to be skipped, there are “gaps” in the markup where these elements would be if not filtered.

That was kind of hard to understand, I admit, but, here, imagine a set of 13 posts like this:

|-\ Post #1
|-\ Post #2
|-\ Post #3
|-\ Post #4
|-\ Post #5
|-\ Post #6
|-\ Post #7
|-\ Post #8
|-\ Post #9
|-\ Post #10
|-\ Post #11
|-\ Post #12
|-\ Post #13

In a Paginator of 6 per page, that would give me 2 pages completely filled and one with just one entry. Perfect.

But let’s say Post #8 now must have the custom Parameter I’ve defined. Post #9 will then occupy its place but Post #13 from the last page WILL NOT occupy the last spot in the second page, I’ll remain with 3 pages and the second will have a “gap” without anything rendered.

And this happens to any Parameter-based conditional defined.

In a list-like Pagination, this isn’t noticeable. But in my theme, I use Bootstrap Cards to show a thumbnail for each entry followed by the Post Title / Summary and the problem does become noticeable.

Is there anything I could do on my end or this is a Hugo issue?

If you follow the advice at Requesting Help and share the site repo, more folks will be able to assist. :slight_smile:

There’s no public repository and all the code that matters it is in the topic. Any paginated theme modified to apply the where as demonstrated is enough. In fact, It’s similar to the subsection Unset Fields of the where docs, since all other examples in there take into account built-in variables like .Section

This “disclaiming comment” has been added only because MANY developers in here don’t read before comment, rejecting “long” topics just because “hey, why the f*ck I’d care”

You need to read the documentation about Pagination. The short version is that you need to filter the pages before you send it to Paginate

Filter it afterwards will make gaps. No surprise there.

Mister, if I came here to ask, rest assured I did read but I didn’t find enough information or what I’ve found wasn’t clear about the problem.

If you mean:

{{ $pages := (where (.Paginator).Pages ".Params.listed" "!=" "false") }}

And later, where the paginated results are actually displayed:

{{ range $offset, $pages }}

That’s what I do.

Now if you mean:

{{ $pages := ( .Paginate (where ".Params.listed" "!=" "false") ).Pages }}

That yields me an error: error calling where: can't iterate over .Params.listed

You are closer in your last example. I suggest you go read about how the where func works.