1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:14:41 -07:00

html/template: simplify ExecuteTemplate a little

Allow the text template to handle the error case of no template
with the given name.
Simplification suggested by Mike Samuel.

R=mikesamuel
CC=golang-dev
https://golang.org/cl/5437147
This commit is contained in:
Rob Pike 2011-12-06 12:47:12 -08:00
parent 66410bac3d
commit ee8b597b1f

View File

@ -47,23 +47,22 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err error) {
return t.text.Execute(wr, data) return t.text.Execute(wr, data)
} }
// ExecuteTemplate applies the template associated with t that has the given name // ExecuteTemplate applies the template associated with t that has the given
// to the specified data object and writes the output to wr. // name to the specified data object and writes the output to wr.
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) (err error) { func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) (err error) {
t.nameSpace.mu.Lock() t.nameSpace.mu.Lock()
tmpl := t.set[name] tmpl := t.set[name]
if tmpl == nil { if (tmpl == nil) != (t.text.Lookup(name) == nil) {
t.nameSpace.mu.Unlock() panic("html/template internal error: template escaping out of sync")
return fmt.Errorf("template: no template %q associated with template %q", name, t.Name())
} }
if !tmpl.escaped { if tmpl != nil && !tmpl.escaped {
err = escapeTemplates(tmpl, name) err = escapeTemplates(tmpl, name)
} }
t.nameSpace.mu.Unlock() t.nameSpace.mu.Unlock()
if err != nil { if err != nil {
return return
} }
return tmpl.text.ExecuteTemplate(wr, name, data) return t.text.ExecuteTemplate(wr, name, data)
} }
// Parse parses a string into a template. Nested template definitions // Parse parses a string into a template. Nested template definitions
@ -106,7 +105,7 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) error {
// Clone is unimplemented. // Clone is unimplemented.
func (t *Template) Clone(name string) error { func (t *Template) Clone(name string) error {
return fmt.Errorf("html/template: Add unimplemented") return fmt.Errorf("html/template: Clone unimplemented")
} }
// New allocates a new HTML template with the given name. // New allocates a new HTML template with the given name.