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

[ENHANCEMENT] Proposed fix to make row pagination count a param instead of hardcoded 5 #277

Closed
Fenugreek opened this issue Jun 11, 2020 · 8 comments · Fixed by #278
Closed

Comments

@Fenugreek
Copy link
Contributor

When site_layout = "row", pagination is currently hardcoded at 5 rows per page. Here's a fix for making the row count a param, row_paginate, instead:

--- a/layouts/partials/row.html
+++ b/layouts/partials/row.html
-{{- $paginator := .Paginate (after 1 (where site.RegularPages "Type" "in" site.Params.mainSections)) 5 -}}
+{{- $paginator := .Paginate (after 1 (where site.RegularPages "Type" "in" site.Params.mainSections)) .Site.Params.row_paginate -}}

Instead of making a pull request, I thought I'd ask for comment here first because there already exists a top level param called paginate used for site_layout = "grid", and it might be better to just use that param for the "row" case as well. I am happy doing that, though I don't know how to access that param (what's the variable name to use? .Site.paginate does not work).

Matt, happy to make a pull request based on your input. Thanks.

@mattstratton
Copy link
Owner

Pretty good idea! Amusingly, it looks like I don't actually use paginate anywhere in the theme; I am pretty sure that the override value is used by the global Paginate object from Hugo. This is how grid.html sets the paginator:

{{ $paginator := .Paginate (where site.RegularPages "Type" "in" site.Params.mainSections) }}

As to how you access it, you would want to do it similar to this (it's important to set a default because folks might not have this set, and we don't want to break existing configs):

{{ $window := $.Site.Params.paginateWindow | default 1 }}

so the code would probably look like this (I haven't tested, so it might not work):

{{- $paginate_value := $.Site.Params.paginate_value | default 5 -}}
{{- $paginator := .Paginate (after 1 (where site.RegularPages "Type" "in" site.Params.mainSections)) $paginate_value -}}

I would slightly recommend rather than creating a new config value, using the existing config. I think that you can get to it with .Site.Paginate (note the use of capitalizations; I am working from memory, but I feel like Hugo cares about this with top-level configuration stuff)

Giving this a little more thought...the best move might be to just change it to:

{{- $paginate_value := $.Site.Paginate | default 5 -}}
{{- $paginator := .Paginate (after 1 (where site.RegularPages "Type" "in" site.Params.mainSections)) $paginate_value -}}

That will preserve the default behavior of 5 rows in a Row layout, but allow the use of the standard paginate configuration setting.

If you want to submit a PR with this change, I'd be happy to take a look!

@Fenugreek
Copy link
Contributor Author

Thanks Matt. I tried your code snippet but it fails at accessing the top-level paginate value:

partials/row.html:115:28: executing "partials/row.html" at <.Site.Paginate>: 
can't evaluate field Paginate in type page.Site

(Strangely, .Site.Title does work to get the top-level title value). I have tried several other permutations (removed capitalization etc.) but had no success.

I am now curious how the paginate value gets accessed in grid.html. (Changing the paginate value does impact the pages served when using site_layout = "grid".) If we know how, we can use the same access here in row.html too.

Thoughts?

@mattstratton
Copy link
Owner

I think that the paginate value is used by the built-in .Paginator object. Did you try $.Site.Paginate instead of .Site.Paginate? The dot can be tricky in Hugo.

I'll experiment with this myself later - not sure if I can get to it today, but in the next day or so!

@mattstratton
Copy link
Owner

mattstratton commented Jun 17, 2020

Reference:
https://gohugo.io/templates/pagination/#configure-pagination

Hugo supports pagination for your homepage, section pages, and taxonomies.

@Fenugreek
Copy link
Contributor Author

I think the following fix (tested and it works) might be best: just using the built-in .Paginator to read in the paginate value. i.e.

--- a/layouts/partials/row.html
+++ b/layouts/partials/row.html

-{{- $paginator := .Paginate (after 1 (where site.RegularPages "Type" "in" site.Params.mainSections)) 5 -}}
+{{- $paginator := .Paginate (after 1 (where site.RegularPages "Type" "in" site.Params.mainSections)) }}

For people using row layout, pagination will change from 5 to
a) 9 if they are using the config.toml file from exampleSite directory, without adjusting the paginate param
b) 10 if they do not have paginate set in their config.toml file (.Paginator defaults to 10)

Behavior a) is unavoidable even if we add logic to default to 5 when paginate is not set (because paginate is set in the example config.toml).

I do not think it is worth being backward compatible to behavior b), as I suspect few users have no paginate in their config.toml.

We can address this by noting this change/functionality in the release notes.

@mattstratton
Copy link
Owner

I agree! If you want to submit a PR, I'll cut a release for it (and you're right, if we just put this in the release notes for this version, that will handle the change, for those folks who read release notes!)

@Fenugreek
Copy link
Contributor Author

Thanks Matt. I'll submit a PR by Saturday.

@Fenugreek
Copy link
Contributor Author

Have submitted PR #278 for your review and action at your convenience. Thanks again. Cheers.

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

Successfully merging a pull request may close this issue.

2 participants