Golang html/template.Template.ExecuteTemplate function examples

package html/template

ExecuteTemplate applies the template associated with t that has the given name to the specified data object and writes the output to wr(1st parameter). If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel.

Golang html/template.Template.ExecuteTemplate function usage examples

Example 1:

 var ie template.HTML = `<!--[if lt IE 9]>
 <script src="js/html5shiv.min.js"></script>
 <script src="js/respond.min.js"></script>
 <![endif]-->`


 func rootHandler(w http.ResponseWriter, r *http.Request) {
  if r.URL.Path != "/" {
 http.NotFound(w, r)
 return
  }
  title := "Home"
  templates.ExecuteTemplate(w, "header.html", &info{title, ie})
  templates.ExecuteTemplate(w, "index.html", nil)
  templates.ExecuteTemplate(w, "footer.html", title)
 }

Example 2:

 var  context interface{}
 var buf *bytes.Buffer = &bytes.Buffer{}
 temp.ExecuteTemplate(buf, "base", context)

References :

https://github.com/aclissold/andrewclissold.appspot.com/blob/master/app.go

http://golang.org/pkg/html/template/#Template.ExecuteTemplate

Advertisement