wr: wr,
vars: []variable{{"$", value}},
}
- t.init()
if t.Tree == nil || t.Root == nil {
state.errorf("%q is an incomplete or empty template%s", t.Name(), t.DefinedTemplates())
}
// it returns the empty string. For generating an error message here
// and in html/template.
func (t *Template) DefinedTemplates() string {
- if t.common == nil {
- return ""
- }
var b bytes.Buffer
for name, tmpl := range t.tmpl {
if tmpl.Tree == nil || tmpl.Root == nil {
// New allocates a new template with the given name.
func New(name string) *Template {
- return &Template{
+ t := &Template{
name: name,
}
+ t.init()
+ return t
}
// Name returns the name of the template.
// delimiters. The association, which is transitive, allows one template to
// invoke another with a {{template}} action.
func (t *Template) New(name string) *Template {
- t.init()
- return &Template{
+ nt := &Template{
name: name,
common: t.common,
leftDelim: t.leftDelim,
rightDelim: t.rightDelim,
}
+ nt.init()
+ return nt
}
func (t *Template) init() {
// 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.common != nil && t.tmpl[name] != nil {
+ if t.tmpl[name] != nil {
return nil, fmt.Errorf("template: redefinition of template %q", name)
}
nt := t.New(name)
// Templates returns a slice of the templates associated with t, including t
// itself.
func (t *Template) Templates() []*Template {
- if t.common == nil {
- return nil
- }
// Return a slice so we don't expose the map.
m := make([]*Template, 0, len(t.tmpl))
for _, v := range t.tmpl {
// type. However, it is legal to overwrite elements of the map. The return
// value is the template, so calls can be chained.
func (t *Template) Funcs(funcMap FuncMap) *Template {
- t.init()
t.muFuncs.Lock()
defer t.muFuncs.Unlock()
addValueFuncs(t.execFuncs, funcMap)
// 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]
}
// (In multiple calls to Parse with the same receiver template, only one call
// can contain text other than space, comments, and template definitions.)
func (t *Template) Parse(text string) (*Template, error) {
- t.init()
t.muFuncs.RLock()
trees, err := parse.Parse(t.name, text, t.leftDelim, t.rightDelim, t.parseFuncs, builtins)
t.muFuncs.RUnlock()