html/template: AddParseTree does not set root template correctly #48477
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?This was also an issue on go1.16. I haven't tested any older releases.
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputAlso reproduces on the Go Playground.
What did you do?
Used
AddParseTree
to set the content of the root/default template then tried to execute it.https://play.golang.org/p/4UPROPWwFFA
What did you expect to see?
Executing the template prints
template content\n
to standard out, like it does for atext/template.Template
created using the same methods.What did you see instead?
Executing the HTML template returns an error:
template: "base" is an incomplete or empty template
.Additional Information
Note that executing the HTML template by name works as expected. This seems to be because the HTML implementation of
AddParseTree
does not set theTree
field of theTemplate
struct. If I manually set this field, it appears to fix the initial error, but this might miss fixing other internal state.I encountered this while building a utility for template inheritance. I used
AddParseTree
becauseClone
does not support changing the template name and I was trying to avoid re-parsing the base template(s) for each leaf in the tree.The text was updated successfully, but these errors were encountered: