To clean up the template folder I would like to save common templates inside a subfolder. Currently I've the following file structure:
main.go
templates/index.tpl # Main template for the main page
templates/includes/head.tpl
templates/includes/footer.tpl
head.tpl
and footer.tpl
will be called within index.tpl
, which looks like this:
{{ template "head" . }}
<h1>My content</h1>
{{ template "footer" .}}
Furthermore the files are parsed using template.ParseGlob()
. Here's an excerpt from main.go
:
var views = template.Must(template.ParseGlob("src/templates/**/*"))
func Render(rw http.ResponseWriter, temp string, data interface{}) {
err := views.ExecuteTemplate(rw, temp, data)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
func Index(rw http.ResponseWriter, req *http.Request) {
Render(rw, "index.tpl", nil)
}
Everytime I open my browser I get this error message: html/template: "index.tpl" is undefined
.
Is it possible, that index.tpl
is ignored with this glob pattern?
I found this similar question, but the answers present only a work around.