Hugo

Link to next branch bundle

Is there some kind of a .NextInSection variable for branch bundles?

I want to create next/previous article links, but when I get to the first/last article in a bundle, I want to link to the neighboring branch bundle. I tried .Parent.NextInSection, but that returns nil, since this only works for regular pages, and the _index.md of a branch bundle is not one of them.

You should be able to do it with Prev/Next as described here I think:

I’m trying, but I can’t quite figure it out.
Here’s a mock-up of my structure:

content
    en
        docs
            settings
                _index.md
                section1
                    _index.md
                    article1.md
                    article2.md
                section2
                    _index.md
                    article1.md
                    article2.md
                    article3.md
                section3   
                    ...

What I want to achieve is that when I reach the end of article2 in section1, I get a link to _index.md in section2.

So I tried {{ with .Parent.Parent.Pages.Prev/Next . }}{{ .Permalink }}{{ end }}, but that returns nil, even though just {{ .Parent.Parent.Pages }} returns Pages(6).

Then I tried {{ with .Parent.Parent.Pages.Next .Parent }}{{ .Permalink }}{{ end }} and I’m getting somewhere, but something’s odd: if I’m in section3/lastArticle.md, I get a permalink to section1/_index.md instead of nil, which is what I’d expect.

What am I doing wrong? I can’t wrap my head around the contexts here ;/

I made an error in my previous post, it’s edited now.

Let’s see if I can wrap … pseudo code:

  • Use .NextInSection to get to article2.md
  • When that is nil, do
{{ $s := $article2.CurrentSection }}
{{ $p := $s.Parent }}
{{ $nextSection := $p.Sections.Prev $s }}
1 Like

OK, after some dabbling, which worked but turned out not to do what I wanted done, I took a completely different approach.

Our page is a wild jumble of logical pages and sections that do not follow Hugo’s normal structures, so I decided to create a slice of the articles I have in the order that I want them (based on the logic that builds our current menus) and I create the next/prev links from the pages stored at the indices before and after the current page.

I’ll have to sit down and re-design this whole documentation someday, but wouldn’t the world be a much better place in general if everyone had the time to break free from technological debt? :wink:

Thanks for the effort anyway, I’ve learned some new things!