I have sections on my site, e.g. /content/cats/ and /content/dogs/.
I have nested sub-sections on my site, e.g. /content/cats/breeds/.
I have pages within those nested sub-sections, e.g. /content/cats/breeds/persian.md
In my template, I want to be able to look up the nested section for the current page. How do I do this? I can’t figure it out.
Both .Section and .CurrentSection seem to return the sections “cats” instead of the section “breeds”. The documentation does say that .Section returns the top-most section, so that’s expected, but it doesn’t say it for .CurrentSection. What am I supposed to use then?
Thanks for the link, but I’ve looked at that page already. As mentioned, .Section and .CurrentSection only return the top-most section, not the current nested section. .Type and .Parent also return the top-most section.
I want to do various things with this information. For example, building my navigation menu (looping over sections and sub-sections), displaying the name of the active sub-section on the page, look up front matter values for the nested section (e.g. params defined in /cats/breeds/_index.md), etc.
It doesn’t seem like there’s any way to get the current nested section from the page, which seems very odd to me. I thought Hugo had support for nested sections now?
/cats/breeds/_index.md defines some variables like: weight = 2 title = "Cat Breeds"
When /cats/breeds/persian.md gets rendered by the template, I want to be able to look up the title of its current nested section (i.e. “Cat Breeds”) and display that somewhere on the page.
When building my site navigation menu, I want to be able to loop over sections (/cats/ and /dogs/, which I can already do easily enough), and then loop over sub-sections (e.g. /cats/breeds/, /cats/caring/, /cats/adoption/) in order as defined by their weight.
When building my site navigation menu, I also want to be able to check if the current page belongs to a given sub-section (in the loop) so that I can add extra CSS classes for the active sub-section.
Is it possible to do any of this in Hugo? I was under the impression that Hugo’s support for nested sections means that subfolders are treated like sections now, in the same way that top-level sections work. But it kind of seems like the “support” is just parsing subfolders for .md files and then lumping them all in the same top-level section. I’m hoping that I’m just misunderstanding something.
Ahh. Sorry for the triple post, but I think I’ve figured it out now.
.CurrentSectiondoes return the nested section after all, as I expected it to.
The problem was that I hadn’t made the /cats/breeds/_index.md file yet, as I was trying to see if Hugo even properly supported this first before going to too much trouble.
It seems like having the _index.md file there in the subdirectory makes Hugo see it as a section, whereas not having it makes it see it as a meaningless subfolder? That’s my take on it at least. I feel like this really needs to be documented better, as I hadn’t seen anything describing this requirement and absolutely nothing made me think of it as something to try.
After creating the _index.md file, suddenly .CurrentSection returns the nested section as I wanted, instead of the top-level section. So this is resolved now. Thanks.