Trying to find the Folder Name for the MD Files

Hello.

I have been trying to pull out the folder name of the md file being processed. The folder structure on my content folder is as follows (sorry for the bad sketch) -
content folder

So basically, there are 7 folders in the content folder, one each for the continents. And these folders have the different related .md files.

Now I have a partial, which is supposed to get the title for the page.

Note: There is no field as “Title” on any of the MD files. And my requirement is to print something like this -
Name of the Continent - Name of the Page
Name of the Page comes in from different fields and that part gets done good. However, I am stuck at the finding the name of the Continent as there is no such field in the MD file set.

I have been trying to get the Folder Name from which the MD file is being processed which is the continent name.

For this I have been trying with the following code in my partial file (named whichfile.html) -

{{ $folderName := replace path.Dir "/" "" }}

But while compiling, I get an error -

Error: Error building site: failed to render pages: render of "page" failed: "E:\Hugo\eth\themes\ethTheme\layouts\_default\baseof.html:3:8": execute of template failed: template: browse/families.html:3:8: executing "browse/families.html" at <partial "head.html" .>: error calling partial: "E:\Hugo\eth\themes\ethTheme\layouts\partials\head.html:18:22": execute of template failed: template: partials/head.html:18:22: executing "partials/head.html" at <partial "whichfile.html" .>: error calling partial: "E:\Hugo\eth\layouts\partials\whichfile.html:17:26": execute of template failed: template: partials/whichfile.html:17:26: executing "partials/whichfile.html" at <path>: wrong number of args for Dir: want 1 got 0

Any ideas what is going wrong?

If you want to get the section name, try this

{{ $folderName := .Section }}

To resolve the error message this needs to be

{{ $folderName := replace .File.Dir "/" "" }}

however, probably what your really want is either what @tut posted (if the continent will always be a top-level folder in the content folder. If the continent might be inside another section, but will always be the section immediately before the page, you could do:

{{ path.Base .CurrentSection.TranslationKey }}

Now if only there were a way to do that as part of a where clause… (bit I digress).

1 Like