Get the slug for the current subsection

This is either a how-to or a support request :slight_smile:

I was trying to get the subsection string/slug of the following file structure:

posts/some-topic/subsection/_index.md
posts/some-topic/subsection/some-other-content.md

In both files I need to know the subsection part of the structure. I thought .Section is the way to go, but that and all other section related functions are focused on some-topic.

After some deep diving I ended up with this construct:

((.CurrentSection).File).ContentBaseName

which returns subsection in all cases as a string.

Is there some variable or method on a page object I am missing or is it really that complicated to get to the subsections slug?

Another way would probably be to use some regexp to extract the last string separated by /*/. Both methods seem overkill.

With v0.123.0 and later…

{{ .CurrentSection.Path | path.Base }}

This construct works regardless of whether the current page is backed by a file (i.e., no need to code defensively).

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.