# Template files - current filename in generated html

**URL:** https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317
**Category:** support
**Created:** [November 15, 2018, 3:20am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317 "2018-11-15T03:20:57Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![rraghur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rraghur/32/5018_2.png) [@rraghur](https://discourse.gohugo.io/u/rraghur)
#### Post date: [November 15, 2018, 3:20am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/1 "2018-11-15T03:20:57Z")

</div>

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:

```auto

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

```

Thanks!

---

<div class="post-metadata">

### Author: ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)
#### Post date: [November 15, 2018, 4:05am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/2 "2018-11-15T04:05:59Z")

</div>

~~You probably want to review [https://gohugo.io/variables/files/](https://gohugo.io/variables/files/) and [https://gohugo.io/templates/files/](https://gohugo.io/templates/files/).~~

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

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [November 15, 2018, 4:19am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/3 "2018-11-15T04:19:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rraghur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rraghur/32/5018_2.png) [@rraghur](https://discourse.gohugo.io/u/rraghur)
#### Post date: [November 15, 2018, 4:40am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/4 "2018-11-15T04:40:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 15, 2018, 5:42am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/5 "2018-11-15T05:42:46Z")

</div>

This is not possible.

---

<div class="post-metadata">

### Author: ![rraghur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rraghur/32/5018_2.png) [@rraghur](https://discourse.gohugo.io/u/rraghur)
#### Post date: [November 15, 2018, 6:28am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/6 "2018-11-15T06:28:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 15, 2018, 11:23am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/7 "2018-11-15T11:23:29Z")

</div>

> [@rraghur](#):
>
> 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](https://github.com/golang/go/issues) – where it probably belongs.

But I would not be too optimistic.

---

<div class="post-metadata">

### Author: ![rraghur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rraghur/32/5018_2.png) [@rraghur](https://discourse.gohugo.io/u/rraghur)
#### Post date: [November 16, 2018, 5:12am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/8 "2018-11-16T05:12:20Z")

</div>

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?

> **[gohugoio/hugo](https://github.com/gohugoio/hugo/compare/master...raghur:dump-template-names)**
>
> The world’s fastest framework for building websites. - gohugoio/hugo

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 16, 2018, 4:30pm UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/9 "2018-11-16T16:30:04Z")

</div>

> [@rraghur](#):
>
> but works as expected

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.

> **[Comparing gohugoio:master...raghur:dump-template-names · gohugoio/hugo](https://github.com/gohugoio/hugo/compare/master...raghur:dump-template-names#diff-d612ae5cdfcc0d7c219d4cf240b6b6cfR237)**
>
> The world’s fastest framework for building websites. - Comparing gohugoio:master...raghur:dump-template-names · gohugoio/hugo

---

<div class="post-metadata">

### Author: ![rraghur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rraghur/32/5018_2.png) [@rraghur](https://discourse.gohugo.io/u/rraghur)
#### Post date: [November 19, 2018, 9:56am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/10 "2018-11-19T09:56:26Z")

</div>

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.

> **[gohugoio/hugo](https://github.com/gohugoio/hugo/compare/master...raghur:dump-template-names#diff-d612ae5cdfcc0d7c219d4cf240b6b6cfR131)**
>
> The world’s fastest framework for building websites. - gohugoio/hugo

---

<div class="post-metadata">

### Author: ![rraghur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rraghur/32/5018_2.png) [@rraghur](https://discourse.gohugo.io/u/rraghur)
#### Post date: [November 20, 2018, 4:36am UTC](https://discourse.gohugo.io/t/template-files-current-filename-in-generated-html/15317/11 "2018-11-20T04:36:49Z")

</div>

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