I don't have enough credit to post a comment to this thread (Is it possible to have nested templates in Go using the standard library? (Google App Engine))
I attempted to follow the selected answer for nesting Go templates, but I still face redefinition of template error.
//contents of base.tmpl
{{define "base"}}
<!DOCTYPE html>
<html>
<head>
{{template "header" .}}
{{template "head" .}}
</head>
<body>
{{template "body" .}}
</body>
</html>
{{end}}
//contents of header.tmpl
{{define "header"}}
<link rel="stylesheet" type="text/css" href="/css/bootstrap/bootstrap.min.css">
{{end}}
//contents of index.tmpl
{{template "base"}}
{{define "head"}}{{end}}
{{define "body"}}
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>Status</th>
<th>Active Hints</th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<td>{{.Id}}</td>
<td>{{.Satus}}</td>
<td>{{.ActiveHints}}</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
//contents of create.tmpl
{{template "base"}}
{{define "head"}}{{end}}
{{define "body"}}
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Question" required>
</div>
</div>
{{end}}
As per the answer to other SO question, I parse each template set separately and execute them separately:
func buildTemplates() {
tmpl := make(map[string]*template.Template)
tmpl["index"] = template.Must(template.ParseFiles("templates/admin/base.tmpl", "templates/admin/index.tmpl", "templates/admin/header.tmpl"))
tmpl["create"] = template.Must(template.ParseFiles("templates/admin/base.tmpl", "templates/admin/create.tmpl", "templates/admin/header.tmpl"))
for _, v := range tmpl {
if err := v.ExecuteTemplate(os.Stdout, "base", nil); err != nil {
log.Fatalf("template execution: %s", err)
}
}
}
It compiles fine, but upon execution: 'panic: template: redefinition of template "body"' What have I done wrong?