Template files - current filename in generated html

Is there a way to get the template filename/realpath during generation? I’m using a sort of complicated theme and it would really help if I could embed the filename as a HTML comment during generation.

I realize that within each template/partial, I could always write out the filename - but this is manual and does not work well if you rename the file etc later.

What I’d like to do:


<!-- template: {{ .Template.File}} --> => <!-- /theme/themename/layout/partial/somefile.html -->
and if the template is overridden then of course: <!-- layout/partial/somefile.html -->

Thanks!

You probably want to review https://gohugo.io/variables/files/ and https://gohugo.io/templates/files/.

Edit: I misread the question, thought template was a verb in that context.

I’m curious about this as well. After some experimenting, it looks like {{ .File.Path }} only works for content files, not template files.

1 Like

@maiki - I don’t see anything on either of those two pages that’ll do the trick. Am I missing something obvious?

This is not possible.

1 Like

Thanks bep… Is this something that’ll qualify for a feature request? If so, please point me to how to file one.

It sounds hard to fix on the Hugo side (not impossible, but hard enought that I’m not doing it on my “hobby time”).

You can try to raise an issue here: Issues · golang/go · GitHub – where it probably belongs.

But I would not be too optimistic.

I took a shot - it’s ugly with downcasts everywhere but works as expected - though I’m not able to get the top level template (section, home etc). Just putting this here since @zwbetz also found this interesting.

Any pointers on where to find the top level page template?

I would expect to see _default/baseof.html if that is the template, which I guess would be the hard part in this.

A quick tip: It would be much more memory effective to write the header to the writer before you execute.

Thank you… I have a quite a stripped down patch now without any of the ugly downcasts. Also, the root template name _default/single.html etc are now rendered

However, running into a weird error where if the template is ‘alias’ then calling fileAndFilename crashes with a panic on L212 at fi, err := fs.Stat(filename)

I’ve got a check and don’t try to resolve the template file name if it’s alias but would obviously like to understand what’s going on.

I’ve filed a ticket at https://github.com/gohugoio/hugo/issues/5453 so its easier to discuss code/pr