Easy way to determine that a page is at the root of a section

I’m trying to figure out a convenient way to determine that a page is at the “root” of a section. Let’s say that I have a documentation site with all the docs in the content/docs directory (which has many subsections). I want some kind of special logic applied only to the page available at /docs (the “root” docs page). How can I construct an $isSectionRoot variable in the layouts/docs/section.html template?

One way I’ve done so is like this:

$isSectionRoot := eq .RelPermalink "/docs/"

This works fine but it’d be nice if there were something more “built in” and also applicable to other sections so that the the comparison URL didn’t need to be hardcoded into the template.

Have any of you confronted this? Any help would be greatly appreciated!

See if this works:

{{ $isSectionRoot := eq .Section (path.Base .File.Dir) }}

Edit: looks like this will only work for traditional pages, not page bundle pages. So the check will have to be made smarter. Currently:
/content/root-section/page-1.md => true
/content/root-section/page-1/index.md => false

1 Like

That’s quite elegant and, indeed, generalizable across sections. Nice! This is actually my first exposure to the path functions, which I will use extensively. I’ll use your trick as a workaround starting now :sunglasses:

Hey @bep, would you be opposed to adding a built-in page variable for this? I’d be happy to do the work but would like to get buy-in first.

{{ $isSectionRoot := in site.Sections $currentPage }}

might also work?

Something like this should work:

{{ if eq .CurrentSection .FirstSection }}
5 Likes

Is .FirstSection missing in the docs (or have I bad eyes)?