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
16 // We use two maps, one for parsing and one for execution.
17 // This separation makes the API cleaner since it doesn't
18 // expose reflection to the client.
20 execFuncs
map[string]reflect
.Value
23 // Template is the representation of a parsed template. The *parse.Tree
24 // field is exported only for use by html/template and should be treated
25 // as unexported by all other clients.
26 type Template
struct {
34 // New allocates a new template with the given name.
35 func New(name
string) *Template
{
41 // Name returns the name of the template.
42 func (t
*Template
) Name() string {
46 // New allocates a new template associated with the given one and with the same
47 // delimiters. The association, which is transitive, allows one template to
48 // invoke another with a {{template}} action.
49 func (t
*Template
) New(name
string) *Template
{
54 leftDelim
: t
.leftDelim
,
55 rightDelim
: t
.rightDelim
,
59 func (t
*Template
) init() {
61 t
.common
= new(common
)
62 t
.tmpl
= make(map[string]*Template
)
63 t
.parseFuncs
= make(FuncMap
)
64 t
.execFuncs
= make(map[string]reflect
.Value
)
68 // Clone returns a duplicate of the template, including all associated
69 // templates. The actual representation is not copied, but the name space of
70 // associated templates is, so further calls to Parse in the copy will add
71 // templates to the copy but not to the original. Clone can be used to prepare
72 // common templates and use them with variant definitions for other templates
73 // by adding the variants after the clone is made.
74 func (t
*Template
) Clone() (*Template
, error
) {
78 for k
, v
:= range t
.tmpl
{
79 if k
== t
.name
{ // Already installed.
82 // The associated templates share nt's common structure.
83 tmpl
:= v
.copy(nt
.common
)
86 for k
, v
:= range t
.parseFuncs
{
89 for k
, v
:= range t
.execFuncs
{
95 // copy returns a shallow copy of t, with common set to the argument.
96 func (t
*Template
) copy(c
*common
) *Template
{
100 nt
.leftDelim
= t
.leftDelim
101 nt
.rightDelim
= t
.rightDelim
105 // AddParseTree creates a new template with the name and parse tree
106 // and associates it with t.
107 func (t
*Template
) AddParseTree(name
string, tree
*parse
.Tree
) (*Template
, error
) {
108 if t
.common
!= nil && t
.tmpl
[name
] != nil {
109 return nil, fmt
.Errorf("template: redefinition of template %q", name
)
117 // Templates returns a slice of the templates associated with t, including t
119 func (t
*Template
) Templates() []*Template
{
123 // Return a slice so we don't expose the map.
124 m
:= make([]*Template
, 0, len(t
.tmpl
))
125 for _
, v
:= range t
.tmpl
{
131 // Delims sets the action delimiters to the specified strings, to be used in
132 // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
133 // definitions will inherit the settings. An empty delimiter stands for the
134 // corresponding default: {{ or }}.
135 // The return value is the template, so calls can be chained.
136 func (t
*Template
) Delims(left
, right
string) *Template
{
142 // Funcs adds the elements of the argument map to the template's function map.
143 // It panics if a value in the map is not a function with appropriate return
144 // type. However, it is legal to overwrite elements of the map. The return
145 // value is the template, so calls can be chained.
146 func (t
*Template
) Funcs(funcMap FuncMap
) *Template
{
148 addValueFuncs(t
.execFuncs
, funcMap
)
149 addFuncs(t
.parseFuncs
, funcMap
)
153 // Lookup returns the template with the given name that is associated with t,
154 // or nil if there is no such template.
155 func (t
*Template
) Lookup(name
string) *Template
{
162 // Parse parses a string into a template. Nested template definitions will be
163 // associated with the top-level template t. Parse may be called multiple times
164 // to parse definitions of templates to associate with t. It is an error if a
165 // resulting template is non-empty (contains content other than template
166 // definitions) and would replace a non-empty template with the same name.
167 // (In multiple calls to Parse with the same receiver template, only one call
168 // can contain text other than space, comments, and template definitions.)
169 func (t
*Template
) Parse(text
string) (*Template
, error
) {
171 trees
, err
:= parse
.Parse(t
.name
, text
, t
.leftDelim
, t
.rightDelim
, t
.parseFuncs
, builtins
)
175 // Add the newly parsed trees, including the one for t, into our common structure.
176 for name
, tree
:= range trees
{
177 // If the name we parsed is the name of this template, overwrite this template.
178 // The associate method checks it's not a redefinition.
183 // Even if t == tmpl, we need to install it in the common.tmpl map.
184 if replace
, err
:= t
.associate(tmpl
, tree
); err
!= nil {
189 tmpl
.leftDelim
= t
.leftDelim
190 tmpl
.rightDelim
= t
.rightDelim
195 // associate installs the new template into the group of templates associated
196 // with t. It is an error to reuse a name except to overwrite an empty
197 // template. The two are already known to share the common structure.
198 // The boolean return value reports wither to store this tree as t.Tree.
199 func (t
*Template
) associate(new *Template
, tree
*parse
.Tree
) (bool, error
) {
200 if new.common
!= t
.common
{
201 panic("internal error: associate not common")
204 if old
:= t
.tmpl
[name
]; old
!= nil {
205 oldIsEmpty
:= parse
.IsEmptyTree(old
.Root
)
206 newIsEmpty
:= parse
.IsEmptyTree(tree
.Root
)
208 // Whether old is empty or not, new is empty; no reason to replace old.
212 return false, fmt
.Errorf("template: redefinition of template %q", name
)