Get Partial Filename

Hey guys.

I’ve been playing with Hugo for a couple of weeks now and built 2 simple sites with it. I’m in the process of creating one more but, while I found lots of answers to other questions in the forum, I can’t find the answer to this one.

Is it possible, and if so how, to retrieve the partial name from within the partial itself?

In the “Variable Scoping” section of the doc on partials it says “[…] This means that the partial will only be able to access those variables”. My understanding is that it should be possible to retrieve the partial name, which is the first of the two variables. However, I can’t figure out how.

Hope this is doable!

Cheers,
Julien

No, the partial name isn’t passed on to the template. You could repeat the template name as a second argument, but that would look funky.

Aw, too bad. Well, I will have to go another way. Thanks for the prompt reply and long live Hugo :wink:

I’d really like to see this capability as well. Here’s a snippet from a partial file named overview.html

{{ if isset .Site.Params "overview" }}
{{ if .Site.Params.overview.enable }}
<section class="bar background-white" id="{{ .Site.Params.overview.id }}">

It would be really nice to be able to access the “overview” filename as variable and use it instead of the hard coded “overview” above. This would enable some partials to be reusable or easier to copy and paste into new partials.

Hi @nevf I don’t follow. You can reuse partials however you’d like, pass in ranges as contexts, and even pass in maps via the dict function. Would you mind expanding for me a bit? Maybe I can I help you out a bit here :smile:

@rdwatters I am very new to both Hugo and Go and am nearing the end of putting a new site together. As I add more partials etc. I’m thinking about re-use and keeping things as simple as possible.

The docs are very good, but I still have trouble finding specific information. For example I’ve read about passing parameters in Go Template Primer and in Partial Templates however these only passes the context of the current page. From your reply I could pass a map,

In this specific case I’d like to know the .Type that the partial is rendering, however it evaluates to 'page' when in fact the type is 'features'

I have /content/home/features folder with a set of markdown files whose frontmatter specifies type = "features"

Now that I’ve written this I have a feeling that what I want to do isn’t possible and possibly doesn’t make sense!