diff options
| author | Rob Pike <r@golang.org> | 2011-12-01 09:19:53 -0800 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2011-12-01 09:19:53 -0800 |
| commit | d38cc47c0c2d830fd745b49bf6be1b0ff0e17b14 (patch) | |
| tree | 7329ea21b94e094bcd797f07a1d3f476f6a433e2 /src/pkg/text | |
| parent | 6bee4e556fddec07cbdeb348dd91d3e55f7e2960 (diff) | |
| download | go-d38cc47c0c2d830fd745b49bf6be1b0ff0e17b14.tar.xz | |
text/template: replace Add with AddParseTree
Makes it clear we're adding exactly one tree and creating a
new template for it.
R=rsc
CC=golang-dev
https://golang.org/cl/5448077
Diffstat (limited to 'src/pkg/text')
| -rw-r--r-- | src/pkg/text/template/multi_test.go | 28 | ||||
| -rw-r--r-- | src/pkg/text/template/template.go | 23 |
2 files changed, 37 insertions, 14 deletions
diff --git a/src/pkg/text/template/multi_test.go b/src/pkg/text/template/multi_test.go index bf4f3078b3..7b35d2633d 100644 --- a/src/pkg/text/template/multi_test.go +++ b/src/pkg/text/template/multi_test.go @@ -10,6 +10,7 @@ import ( "bytes" "fmt" "testing" + "text/template/parse" ) type isEmptyTest struct { @@ -258,3 +259,30 @@ func TestClone(t *testing.T) { t.Errorf("expected %q got %q", "bclone", b.String()) } } + +func TestAddParseTree(t *testing.T) { + // Create some templates. + root, err := New("root").Parse(cloneText1) + if err != nil { + t.Fatal(err) + } + _, err = root.Parse(cloneText2) + if err != nil { + t.Fatal(err) + } + // Add a new parse tree. + tree, err := parse.Parse("cloneText3", cloneText3, "", "", nil, builtins) + if err != nil { + t.Fatal(err) + } + added, err := root.AddParseTree("c", tree["c"]) + // Execute. + var b bytes.Buffer + err = added.ExecuteTemplate(&b, "a", 0) + if err != nil { + t.Fatal(err) + } + if b.String() != "broot" { + t.Errorf("expected %q got %q", "broot", b.String()) + } +} diff --git a/src/pkg/text/template/template.go b/src/pkg/text/template/template.go index c1d0c1c349..04fca407c1 100644 --- a/src/pkg/text/template/template.go +++ b/src/pkg/text/template/template.go @@ -103,21 +103,16 @@ 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) +// AddParseTree creates a new template with the name and parse tree +// and associates it with t. +func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) { + if t.tmpl[name] != nil { + return nil, fmt.Errorf("template: redefinition of template %q", name) } - arg.common = t.common - t.tmpl[arg.name] = arg - return nil + nt := t.New(name) + nt.Tree = tree + t.tmpl[name] = nt + return nt, nil } // Templates returns a slice of the templates associated with t, including t |
