diff options
Diffstat (limited to 'libgo/go/html/template/template.go')
-rw-r--r-- | libgo/go/html/template/template.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/libgo/go/html/template/template.go b/libgo/go/html/template/template.go index bb9140a..96ab268 100644 --- a/libgo/go/html/template/template.go +++ b/libgo/go/html/template/template.go @@ -17,7 +17,7 @@ import ( // Template is a specialized Template from "text/template" that produces a safe // HTML document fragment. type Template struct { - // Sticky error if escaping fails. + // Sticky error if escaping fails, or escapeOK if succeeded. escapeErr error // We could embed the text/template field, but it's safer not to because // we need to keep our version of the name space and the underlying @@ -80,7 +80,7 @@ func (t *Template) escape() error { defer t.nameSpace.mu.Unlock() if t.escapeErr == nil { if t.Tree == nil { - return fmt.Errorf("template: %q is an incomplete or empty template%s", t.Name(), t.text.DefinedTemplates()) + return fmt.Errorf("template: %q is an incomplete or empty template%s", t.Name(), t.DefinedTemplates()) } if err := escapeTemplate(t, t.text.Root, t.Name()); err != nil { return err @@ -143,6 +143,13 @@ func (t *Template) lookupAndEscapeTemplate(name string) (tmpl *Template, err err return tmpl, err } +// DefinedTemplates returns a string listing the defined templates, +// prefixed by the string "; defined templates are: ". If there are none, +// it returns the empty string. Used to generate an error message. +func (t *Template) DefinedTemplates() string { + return t.text.DefinedTemplates() +} + // Parse parses a string into a template. Nested template definitions // will be associated with the top-level template t. Parse may be // called multiple times to parse definitions of templates to associate @@ -169,6 +176,8 @@ func (t *Template) Parse(src string) (*Template, error) { tmpl := t.set[name] if tmpl == nil { tmpl = t.new(name) + } else if tmpl.escapeErr != nil { + return nil, fmt.Errorf("html/template: cannot redefine %q after it has executed", name) } // Restore our record of this text/template to its unescaped original state. tmpl.escapeErr = nil @@ -228,6 +237,7 @@ func (t *Template) Clone() (*Template, error) { set: make(map[string]*Template), }, } + ret.set[ret.Name()] = ret for _, x := range textClone.Templates() { name := x.Name() src := t.set[name] @@ -413,3 +423,10 @@ func parseGlob(t *Template, pattern string) (*Template, error) { } return parseFiles(t, filenames...) } + +// IsTrue reports whether the value is 'true', in the sense of not the zero of its type, +// and whether the value has a meaningful truth value. This is the definition of +// truth used by if and other such actions. +func IsTrue(val interface{}) (truth, ok bool) { + return template.IsTrue(val) +} |