Sunday, 15 February 2015

go - Golang Nested Template redefinition error -



go - Golang Nested Template redefinition error -

i don't have plenty credit post comment thread (is possible have nested templates in go using standard library? (google app engine))

i attempted follow selected reply nesting go templates, 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 reply other question, parse each template set separately , 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")) _, v := range tmpl { if err := v.executetemplate(os.stdout, "base", nil); err != nil { log.fatalf("template execution: %s", err) } } }

it compiles fine, upon execution: 'panic: template: redefinition of template "body"' have done wrong?

templates go nested

No comments:

Post a Comment