Hugo: .{Next,Prev}InSection should support and prefer weight to date

Created on 25 Sep 2016  路  13Comments  路  Source: gohugoio/hugo

I have content which is much more important to sort and traverse in the UI by the weight of the content than the data the files were made. Currently, in my theme's _default/single.html page, I am displaying navigational controls which go to the .{Next,Prev}InSection but that is based on the date parameter for the content instead of the weight parameter.

This is frustrating because on the corresponding _default/list.html the default ordering for content in the list is first by weight and then by date. I have hacked around this by writing a script that converts weights to similarly ordered dates and then adds date fields to each content file, but that seems like something I shouldn't ask of consumers of my theme.

I'm not sure if this is a bug or an enhancement. I guess it is the later since the documentation is, at least, very clear on the inconsistency between the default order of the items in the range function and the order used by .{Next,Prev}InSection. Still, it would be a great enhancement to make them consistent.

Enhancement Stale

Most helpful comment

Please, reopen this issue.

All 13 comments

Are you sure about that not being the current case?

There isn't solid automated tests in that area, but when I skim the code, the sort order seems to be:

  1. weight
  2. date
  3. title

If that is not the case, I would say it is clearly a bug and should be fixed.

Never mind -- I see now, it is modeled as a taxonomy (with its own weight), which is probably a bad choice.

Oh, then, does that mean that as a workaround, I could also have a "sections_weight" field which is set to the same value as "weight" and the ordering would work as expected?

I could also have a "sections_weight" field which is set to the same value as "weight" and the ordering would work as expected?

That should work, yes.

So, it's very strange. Using sections_weight seems to have worked, but in exactly the opposite sort order of the items listed in the Section list. That is, on the layouts/default/_list.html page when I use for range .Data.Pages I get them in the weight order "10, 20, 25, 30, 40, 50" But, when I am rendering the page for a single one of those pages (let's say the one with sections_weight = 30 I find that the value for .NextInSection.PermaLink is the permalink for the page with sections_weight = 25 and .PrevInSection.PermaLink is the permalink for the page with sections_weight = 40 I would have expected exactly the opposite to be true.

Also, I saw from the documentation a recommended idiom of:
{{if .PrevInSection}}{{.PrevInSection.Permalink}}{{end}} but, I find that even when I am on the last item in the section, both .PrevInSection and .NextInSection have non-empty values. So, either way, I get a link. Although, one of the links is to the same page. Obviously, I can hack around this, but I am doubtful it is the intended behavior. Maybe I should open a separate issue?

This issue has been automatically marked as stale because it has not been commented on for at least four months.

The resources of the Hugo team are limited, and so we are asking for your help.

If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.

If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.

This issue will automatically be closed in four months if no further activity occurs. Thank you for all your contributions.

Note/Update: This issue is marked as stale, and I may have said something earlier about "opening a thread on the discussion forum". Please don't.

If this is a bug and you can still reproduce this error on the latest release or the master branch, please reply with all of the information you have about it in order to keep the issue open.

If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.

I would like to suggest we keep this open as a feature request.

Right now, .Prev, .Next, .NextInSection, and .PrevInSection don't mirror the default ordering used in other areas of Hugo. As such, they have limited use without workarounds/hacks and often mean extra(neous) fields added to front matter. Perhaps we can open this up for feedback?

I think it is time to "clean up this" slightly fragmented code base.

We currently have:

  1. Prev, Next, which is based on the .RegularPages collection, correctly sorted by weight and then date etc., but goes in the wrong direction in my head.
  2. PrevInSection etc. goes in the correct direction, but is based on the Section taxonomy, which is sorted by taxonomy weight and then by date ...
  3. Then there are the Prev and Next on the different page collections; it goes in the wrong direction, it needs a current page as argument, and it goes around and around, and I suspect there are some issues with non-content-file types, but it is flexible -- i.e. you can get whatever sorting you want.

So, if someone could fix 3) that would probably be the best.

Is sections_weight worth documenting?

In my self-written hugo theme (adapted from "Beautiful Hugo") I want to make the "Previous" and "Next" buttons for single content pages work like so: I'd like to add "weight" to my front matter and I'd expect pages with lower weights to appear "Previous" to higher weights.

The current implementation of the theme uses .PrevInSection and .NextInSection to implement those buttons.

I spent more than three hours now trying to figure out how .Prev/NextInSection works. All I could figure out was:

  • weight is not regarded unless date in the front matter is equal for all pages in a section.
  • weight is treated counter-intuitive: Higher weights apper previous to lower weights.
  • Information that you can find about .Prev/NextInSection on the web is poor and contradictious.
  • I did not manage to get the sections_weight (described above) to work. Might be its because
    that I wasn't able to grasp how it works from the comments above
  • hugo server in combination with chrome showing "localhost:1313" misbehaves badly when changing
    anything with an impact on .Prev/NextInSection (such as the weight, date or file name).
    For each of my tries I needed to shutdown hugo server and delete all browser contents to
    make sure to get reproducable results.

Could someone shed some reliable light on what .Prev/NextInSection does and how to correctly
make it obey to the pages' weights? Thank you.

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

Please, reopen this issue.

Was this page helpful?
0 / 5 - 0 ratings