I'm currently trying to create a small web application using golang and I'm following the tutorial on their webpage (https://golang.org/doc/articles/wiki/final.go). Instead of having the templates on the same folder as the rest of the code I'm trying to move them into templates/template_name.html
.
For template rendering I'm using the following code:
var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html"))
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
And for example my View handler is the following:
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
return
}
renderTemplate(w, "view", p)
}
I have the templates/
folder with the edit.html
and view.html
file inside. I run the full code with: go run wiki.go
but when I try to access the webpage I get the following error:
html/template: "templates/view.html" is undefined
Any idea of what might be happening?