Skip to content

text/template: AddParseTree can't replace empty templates, whereas Parse can #10990

@chowey

Description

@chowey

Given that I have an empty "test" template:

tpl, err := template.New("main").Parse(`
    {{template "test" .}}
    {{define "test"}}{{end}}
`)

this works:

tpl, err = tpl.New("test").Parse(`This is a test`)
// no error

and this fails:

test, err := template.New("test").Parse(`This is a test`)
tpl, err = tpl.AddParseTree("test", test.Tree)
// error

I think the AddParseTree conditional should be changed to:

    if t.common != nil
        if old := t.tmpl[name]; old != nil && !parse.IsEmptyTree(old.Root) {
            return nil, fmt.Errorf("template: redefinition of template %q", name)
        }
    }

This would make the behavior of AddParseTree consistent with Parse.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions