1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // Template is the representation of a parsed template.
14 type Template struct {
17 // We use two maps, one for parsing and one for execution.
18 // This separation makes the API cleaner since it doesn't
19 // expose reflection to the client.
21 execFuncs map[string]reflect.Value
22 set *Set // can be nil.
25 // Name returns the name of the template.
26 func (t *Template) Name() string {
32 // New allocates a new template with the given name.
33 func New(name string) *Template {
36 parseFuncs: make(FuncMap),
37 execFuncs: make(map[string]reflect.Value),
41 // Funcs adds the elements of the argument map to the template's function
42 // map. It panics if a value in the map is not a function with appropriate
44 // The return value is the template, so calls can be chained.
45 func (t *Template) Funcs(funcMap FuncMap) *Template {
46 addValueFuncs(t.execFuncs, funcMap)
47 addFuncs(t.parseFuncs, funcMap)
51 // Parse parses the template definition string to construct an internal
52 // representation of the template for execution.
53 func (t *Template) Parse(s string) (tmpl *Template, err os.Error) {
54 t.Tree, err = parse.New(t.name).Parse(s, t.parseFuncs, builtins)
61 // ParseInSet parses the template definition string to construct an internal
62 // representation of the template for execution. It also adds the template
64 // Function bindings are checked against those in the set.
65 func (t *Template) ParseInSet(s string, set *Set) (tmpl *Template, err os.Error) {
68 setFuncs = set.parseFuncs
70 t.Tree, err = parse.New(t.name).Parse(s, t.parseFuncs, setFuncs, builtins)
78 // addToSet adds the template to the set, verifying it's not being double-assigned.
79 func (t *Template) addToSet(set *Set) {
80 if set == nil || t.set == set {
83 // If double-assigned, Add will panic and we will turn that into an error.