I'm passing a struct to a nested template and trying to render a different template based on a field in that struct.
func myHandler(w http.ResponseWriter, r *http.Request) {
allContent := getContent()
response := []struct {
Title string
Content []Content
Form string
}{
{"BIG TITLE, allContent.bigContent, "big_form"},
{"SMALL TITLE, allContent.smallContent, "small_form"},
}
t, err := template.ParseFiles("content.html", "big_form.html", "small_form.html")
if err != nil {
log.Printf("Failed to parse template: %v", err)
}
if err := t.Execute(w, response); err != nil {
log.Printf("Failed to execute template: %v", err)
}
}
and the template:
{{range .}} {{$form := .Form}}
<h1 class="blocks-header inlined">{{.Title}}</h1>
{{range .Content}}
<p>{{.Something}}</p>
{{template $form .}}
{{end}} {{end}}
The $form
variable renders as a string when used on it's own (<p>{{$form}}</p>
) but when used as the template name I get the error: unexpected "$form" in template clause
How can I pass the template name in a way the template clause will understand?