Given a set of templates like:
layout.tpl
<html>
<head>
<title>Some title</title>
{{template extracss}}
</head>
<body>
<h1>Page title</h1>
{{template content .}}
</body>
</html>
home.tpl
{{define "content"}}
<p>page content goes here</p>
{{end}}
edit.tpl
{{define "content"}}
<form>form content goes here</form>
{{end}}
{{define "extracss"}}<style>body{background:pink}</style>{{end}}
using this to render the template:
func Render(w http.ResponseWriter, tmpname string, data interface{}) {
t, err := template.ParseFiles("views/layout.tpl", "views/"+tmpname+".tpl")
// parse error
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
if err := t.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
edit.tpl will render correctly as it has 'extracss' defined, home.tpl will not as the template parser rightly says 'no such template "extracss"'.
So what mechanism would I use to allow 'optional' templates to be used?
Any ideas?