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 // common holds the information shared by related templates.
15 tmpl map[string]*Template // Map from name to defined templates.
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.
20 muFuncs sync.RWMutex // protects parseFuncs and execFuncs
22 execFuncs map[string]reflect.Value
25 // Template is the representation of a parsed template. The *parse.Tree
26 // field is exported only for use by html/template and should be treated
27 // as unexported by all other clients.
28 type Template struct {
36 // New allocates a new, undefined template with the given name.
37 func New(name string) *Template {
45 // Name returns the name of the template.
46 func (t *Template) Name() string {
50 // New allocates a new, undefined template associated with the given one and with the same
51 // delimiters. The association, which is transitive, allows one template to
52 // invoke another with a {{template}} action.
54 // Because associated templates share underlying data, template construction
55 // cannot be done safely in parallel. Once the templates are constructed, they
56 // can be executed in parallel.
57 func (t *Template) New(name string) *Template {
62 leftDelim: t.leftDelim,
63 rightDelim: t.rightDelim,
68 // init guarantees that t has a valid common structure.
69 func (t *Template) init() {
72 c.tmpl = make(map[string]*Template)
73 c.parseFuncs = make(FuncMap)
74 c.execFuncs = make(map[string]reflect.Value)
79 // Clone returns a duplicate of the template, including all associated
80 // templates. The actual representation is not copied, but the name space of
81 // associated templates is, so further calls to Parse in the copy will add
82 // templates to the copy but not to the original. Clone can be used to prepare
83 // common templates and use them with variant definitions for other templates
84 // by adding the variants after the clone is made.
85 func (t *Template) Clone() (*Template, error) {
91 for k, v := range t.tmpl {
96 // The associated templates share nt's common structure.
97 tmpl := v.copy(nt.common)
101 defer t.muFuncs.RUnlock()
102 for k, v := range t.parseFuncs {
105 for k, v := range t.execFuncs {
111 // copy returns a shallow copy of t, with common set to the argument.
112 func (t *Template) copy(c *common) *Template {
116 nt.leftDelim = t.leftDelim
117 nt.rightDelim = t.rightDelim
121 // AddParseTree adds parse tree for template with given name and associates it with t.
122 // If the template does not already exist, it will create a new one.
123 // If the template does exist, it will be replaced.
124 func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
126 // If the name is the name of this template, overwrite this template.
131 // Even if nt == t, we need to install it in the common.tmpl map.
132 if t.associate(nt, tree) || nt.Tree == nil {
138 // Templates returns a slice of defined templates associated with t.
139 func (t *Template) Templates() []*Template {
143 // Return a slice so we don't expose the map.
144 m := make([]*Template, 0, len(t.tmpl))
145 for _, v := range t.tmpl {
151 // Delims sets the action delimiters to the specified strings, to be used in
152 // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
153 // definitions will inherit the settings. An empty delimiter stands for the
154 // corresponding default: {{ or }}.
155 // The return value is the template, so calls can be chained.
156 func (t *Template) Delims(left, right string) *Template {
163 // Funcs adds the elements of the argument map to the template's function map.
164 // It must be called before the template is parsed.
165 // It panics if a value in the map is not a function with appropriate return
166 // type or if the name cannot be used syntactically as a function in a template.
167 // It is legal to overwrite elements of the map. The return value is the template,
168 // so calls can be chained.
169 func (t *Template) Funcs(funcMap FuncMap) *Template {
172 defer t.muFuncs.Unlock()
173 addValueFuncs(t.execFuncs, funcMap)
174 addFuncs(t.parseFuncs, funcMap)
178 // Lookup returns the template with the given name that is associated with t.
179 // It returns nil if there is no such template or the template has no definition.
180 func (t *Template) Lookup(name string) *Template {
187 // Parse parses text as a template body for t.
188 // Named template definitions ({{define ...}} or {{block ...}} statements) in text
189 // define additional templates associated with t and are removed from the
190 // definition of t itself.
192 // Templates can be redefined in successive calls to Parse.
193 // A template definition with a body containing only white space and comments
194 // is considered empty and will not replace an existing template's body.
195 // This allows using Parse to add new named template definitions without
196 // overwriting the main template body.
197 func (t *Template) Parse(text string) (*Template, error) {
200 trees, err := parse.Parse(t.name, text, t.leftDelim, t.rightDelim, t.parseFuncs, builtins)
205 // Add the newly parsed trees, including the one for t, into our common structure.
206 for name, tree := range trees {
207 if _, err := t.AddParseTree(name, tree); err != nil {
214 // associate installs the new template into the group of templates associated
215 // with t. The two are already known to share the common structure.
216 // The boolean return value reports whether to store this tree as t.Tree.
217 func (t *Template) associate(new *Template, tree *parse.Tree) bool {
218 if new.common != t.common {
219 panic("internal error: associate not common")
221 if old := t.tmpl[new.name]; old != nil && parse.IsEmptyTree(tree.Root) && old.Tree != nil {
222 // If a template by that name exists,
223 // don't replace it with an empty template.
226 t.tmpl[new.name] = new