]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: address a couple of issues for html/template
authorRob Pike <r@golang.org>
Mon, 28 Nov 2011 18:42:57 +0000 (10:42 -0800)
committerRob Pike <r@golang.org>
Mon, 28 Nov 2011 18:42:57 +0000 (10:42 -0800)
- allow Lookup to work on uninitialized templates
- fix bug in add: can't error after parser is stopped
- add Add method for html/template

R=adg, rogpeppe, r, rsc
CC=golang-dev
https://golang.org/cl/5436080

src/pkg/text/template/parse/parse.go
src/pkg/text/template/template.go

index 36c54032ac6817febf1c30d6eac8ed7c6eef6de2..346f613b048398e8bda870c62a6998a73085c45b 100644 (file)
@@ -170,8 +170,8 @@ func (t *Tree) Parse(s, leftDelim, rightDelim string, treeSet map[string]*Tree,
        defer t.recover(&err)
        t.startParse(funcs, lex(t.Name, s, leftDelim, rightDelim))
        t.parse(treeSet)
-       t.stopParse()
        t.add(treeSet)
+       t.stopParse()
        return t, nil
 }
 
index aa559fa8afcbf831c239f556bf2f9a7207be6eb3..c1d0c1c3491a178916258c02c65563fd59c01106 100644 (file)
@@ -103,6 +103,23 @@ func (t *Template) copy(c *common) *Template {
        return nt
 }
 
+// Add associates the argument template, arg, with t, and vice versa,
+// so they may invoke each other. To do this, it also removes any
+// prior associations arg may have. Except for losing the link to
+// arg, templates associated with arg are otherwise unaffected. It
+// is an error if the argument template's name is already associated
+// with t.  Add is here to support html/template and is not intended
+// for other uses.
+// TODO: make this take a parse.Tree argument instead of a template.
+func (t *Template) Add(arg *Template) error {
+       if t.tmpl[arg.name] != nil {
+               return fmt.Errorf("template: redefinition of template %q", arg.name)
+       }
+       arg.common = t.common
+       t.tmpl[arg.name] = arg
+       return nil
+}
+
 // Templates returns a slice of the templates associated with t, including t
 // itself.
 func (t *Template) Templates() []*Template {
@@ -139,6 +156,9 @@ func (t *Template) Funcs(funcMap FuncMap) *Template {
 // Lookup returns the template with the given name that is associated with t,
 // or nil if there is no such template.
 func (t *Template) Lookup(name string) *Template {
+       if t.common == nil {
+               return nil
+       }
        return t.tmpl[name]
 }