Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to partition (page) slices #12830

Open
raoulb opened this issue Sep 4, 2024 · 5 comments
Open

Add functions to partition (page) slices #12830

raoulb opened this issue Sep 4, 2024 · 5 comments
Labels
Milestone

Comments

@raoulb
Copy link
Contributor

raoulb commented Sep 4, 2024

I propose to add some new functionality to split a slice of pages into chunks of a given fixed length plus possibly a shorter remainder.

Use cases include listings and multi-column row-based tabular layouts.

As far as I know there is right now no such functionality exposed to the templates API. The new functions should be straight-forward to use compared to a custom implementation by lower-level template primitives. It is expected to be faster and less error-prone too and, as a built-in readily available to all Hugo users.

This topic came up first with the bug report #11553 and it solves the issue raised there.
That bug is related to #11131 which broke a custom template-based implementation.

An implementation can be found at PR #12661 (resp. the branch https://github.com/raoulb/hugo/tree/partition).

With this post I'd like to start the discussion on what needs to be changed to get this functionality merged.

@jmooring
Copy link
Member

jmooring commented Sep 5, 2024

Please provide an example of how you would use the new template function in a template (e.g., /layouts/_default/list.html).

@bep bep removed the NeedsTriage label Sep 8, 2024
@bep bep added this to the Unscheduled milestone Sep 8, 2024
@bep
Copy link
Member

bep commented Sep 8, 2024

Thanks for this; this needs to put on hold for a little. I need to think a little about the "future" of the collections package. This package was implemented before Go generics, and I don't want to add more complexity/generics code if we don't need to. For funcs that operate on slices only, e.g. Partition, I don't see why we cannot just delegate to something ala:

https://pkg.go.dev/github.com/bep/helpers@v0.5.0/slicehelpers

@raoulb
Copy link
Contributor Author

raoulb commented Sep 9, 2024

Please provide an example of how you would use the new template function in a template (e.g., /layouts/_default/list.html).

I made a minimal example which can be found here

An envisioned use case might look like:

  {{ range $.Site.RegularPages | partition 3 | first 2 }}
    <h3>Some heading</h3>
    <div>
      {{ partial "fancy.html" . }}
    </div>
  {{ end }}

where the fancy.html partial might implement a complex layout like a slideshow or similar.

The minimal example here will look like:

slideshow

@raoulb
Copy link
Contributor Author

raoulb commented Sep 9, 2024

Thanks for this; this needs to put on hold for a little. I need to think a little about the "future" of the collections package. This package was implemented before Go generics, and I don't want to add more complexity/generics code if we don't need to. For funcs that operate on slices only, e.g. Partition, I don't see why we cannot just delegate to something ala:

https://pkg.go.dev/github.com/bep/helpers@v0.5.0/slicehelpers

I'd be fine with skipping the collections part (i.e. partition) and for now just add the PartitionWith function on Pages.

@raoulb
Copy link
Contributor Author

raoulb commented Sep 9, 2024

For funcs that operate on slices only, e.g. Partition, I don't see why we cannot just delegate to something ala:

https://pkg.go.dev/github.com/bep/helpers@v0.5.0/slicehelpers

I guess we can? I'd try to implement this once we decide to use that dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants