// the output writer.
// A template may be executed safely in parallel.
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
- tmpl := t.tmpl[name]
+ var tmpl *Template
+ if t.common != nil {
+ tmpl = t.tmpl[name]
+ }
if tmpl == nil {
return fmt.Errorf("template: no template %q associated with template %q", name, t.name)
}
// 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 {
func TestExecuteOnNewTemplate(t *testing.T) {
// This is issue 3872.
- _ = New("Name").Templates()
+ New("Name").Templates()
+ // This is issue 11379.
+ new(Template).Templates()
+ new(Template).Parse("")
+ new(Template).New("abc").Parse("")
+ new(Template).Execute(nil, nil) // returns an error (but does not crash)
+ new(Template).ExecuteTemplate(nil, "XXX", nil) // returns an error (but does not crash)
}
const testTemplates = `{{define "one"}}one{{end}}{{define "two"}}two{{end}}`
// contents before calling ParseFiles, t.Execute may fail. In that
// case use t.ExecuteTemplate to execute a valid template.
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
+ t.init()
return parseFiles(t, filenames...)
}
// equivalent to calling t.ParseFiles with the list of files matched by the
// pattern.
func (t *Template) ParseGlob(pattern string) (*Template, error) {
+ t.init()
return parseGlob(t, pattern)
}
// 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()
nt := &Template{
name: name,
common: t.common,
func (t *Template) Clone() (*Template, error) {
nt := t.copy(nil)
nt.init()
+ if t.common == nil {
+ return nt, nil
+ }
for k, v := range t.tmpl {
if k == t.name {
nt.tmpl[t.name] = nt
// If the template does not already exist, it will create a new one.
// It is an error to reuse a name except to overwrite an empty template.
func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
+ t.init()
// If the name is the name of this template, overwrite this template.
// The associate method checks it's not a redefinition.
nt := t
// Templates returns a slice of defined templates associated with t.
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 {
// corresponding default: {{ or }}.
// The return value is the template, so calls can be chained.
func (t *Template) Delims(left, right string) *Template {
+ t.init()
t.leftDelim = left
t.rightDelim = right
return t
// 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.
// It returns nil if there is no such template or the template has no definition.
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()