Getting the Parent name from a template `.md`

I’m looking to find the parameter to get the parent’s folder name.

docs/folder/ is generated as docs/folder/subpage no issue there. But it’s not nested in the toc. For that to work I need to figure out how to get the /folder/ part from the ChatGPT suggested a complex string splitter setup after I couldn’t find something suitable in the docs. Which is something I’d rather avoid if there’s a direct solution.

It goes here btw:

    parent: "foldername"

It’d be great if the pieces were properly nested in the toc without manual intervention. I just couldn’t figure out how.

You refer to a table of contents (TOC), but your example is a menu entry.

Are you trying to create a section menu?

That’s a definite probably. It’s a listing of /docs/

I’m getting the impression that my understanding of a TOC doesn’t match Hugo’s definition of a TOC. I mean whatever gets me the parent dir name :slight_smile:

A table of contents is a list of heading elements on a given page.

A menu is a list of links to pages, generated by (a) Hugo’s built-in menu system, or (b) recursively walking the directory structure (often referred to as a section menu).

If you want to use Hugo’s built-in menu system, the parent property of the menu entry needs to point to the identifer or name property of its parent menu entry.


I see. This one is pretty much my situation: Menus | Hugo

They have:

      class: center
    parent: Products
    pre: <i class="fa-solid fa-code"></i>
    weight: 20
title: Software

I’m looking for something that gets me the parent value (in the example Products) upon creation of the page via hugo new folder/ as at that point Hugo knows both the parent and the child.

So, if I understand you correctly, you do not want to create a menu entry in site configuration for each page that you add to content. Instead, you want your navigation to be updated whenever you create a new page.

Is that correct?

Yup that would be an apt assessment. I create a new sub-page and the sub-page has the correct value put in the parent: automatically based on its parent section/folder.

Take this for a spin:

git clone --single-branch -b hugo-forum-topic-42835 hugo-forum-topic-42835
cd hugo-forum-topic-42835
hugo server

Thanks for that. The archetypes there are basic, there’s nothing there to indicate any sort of nesting. I can probably do it with a Bash script though since I batch generate the documentation section, I have the top category of each in the script as a variable anyway. Would’ve been nice to have it native though.


Yes. Look in the archetypes folder. There is no parent information there. Each post is created in a void without any relation to where it belongs.

The in-page list has nothing to do with what I’m looking for. What I was after is a way to get the foldername part into the parent frontmatter when hugo new foldername/ as I have many in fewer foldername. Hence the archetypes/ should have something in the parent: that tells the page where it belongs to in terms of nesting.

Structural result example:

  • foldername

By default, Hugo doesn’t know the filename parts belong to foldername therefore the need for setting the parent: in frontmatter. I need to do this a good couple of times, so automating it is fairly necessary.

You’re on your own.

OK. Thanks for trying though.

Found it.

This goes in the frontmatter parent = "{{ path.Base .File.Dir }}"

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