Solution to html/template: “your-template.tmpl” is undefined

TL;DR

First I thought when I execute a template I’m using relative filepath, but I was wrong because it actually using template name, which is the base file name (without the directory).

Longer explanation

While following the awe…


This content originally appeared on DEV Community and was authored by iamlearning

TL;DR

First I thought when I execute a template I'm using relative filepath, but I was wrong because it actually using template name, which is the base file name (without the directory).

Longer explanation

While following the awesome Golang wiki page assignment, I had hard time with putting the template files into a subdirectory and make them work.

If you are here, you might have some of the following directory:

├── IAmNew.txt
├── TestPage.txt
├── go.mod
├── tmpl
│   ├── page-edit.html
│   └── page-view.html
└── wiki.go

And you have something similar code to this:

var templates = template.Must(template.ParseGlob("./tmpl/*.html"))

or maybe you used the ParseFiles and have a list of files instead:

var templates = template.Must(template.ParseGlob("tmpl/page-view.html", "tmpl/page-edit.html"))

⚠️ Wrong way of executing templates

err := templates.ExecuteTemplate(w, "tmpl/"+tmpl+".html", p)

✅ I removed the directory prefix and it worked

err := templates.ExecuteTemplate(w, tmpl+".html", p)

Explanation from the documentation itself

ParseFiles creates a new Template and parses the template definitions from the named files. The returned template's name will have the (base) name and (parsed) contents of the first file. There must be at least one file.
If an error occurs, parsing stops and the returned *Template is nil.

Also another possible reason could be

When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results. For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template named "foo", while "a/foo" is unavailable.


This content originally appeared on DEV Community and was authored by iamlearning


Print Share Comment Cite Upload Translate Updates
APA

iamlearning | Sciencx (2021-08-05T23:43:34+00:00) Solution to html/template: “your-template.tmpl” is undefined. Retrieved from https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/

MLA
" » Solution to html/template: “your-template.tmpl” is undefined." iamlearning | Sciencx - Thursday August 5, 2021, https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/
HARVARD
iamlearning | Sciencx Thursday August 5, 2021 » Solution to html/template: “your-template.tmpl” is undefined., viewed ,<https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/>
VANCOUVER
iamlearning | Sciencx - » Solution to html/template: “your-template.tmpl” is undefined. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/
CHICAGO
" » Solution to html/template: “your-template.tmpl” is undefined." iamlearning | Sciencx - Accessed . https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/
IEEE
" » Solution to html/template: “your-template.tmpl” is undefined." iamlearning | Sciencx [Online]. Available: https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/. [Accessed: ]
rf:citation
» Solution to html/template: “your-template.tmpl” is undefined | iamlearning | Sciencx | https://www.scien.cx/2021/08/05/solution-to-html-template-your-template-tmpl-is-undefined/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.