Using templates with delimiters works fine when using template.New("...").Delims("[[", "]]").Parse()
However, I cannot figure out how to get to the same result with template.ParseFiles()
tmpl, err := template.ParseFiles("base.tmpl", "homepage/inner.tmpl")
if err != nil { panic(err) }
tmpl.Delims("[[", "]]")
p := new(Page)
err = tmpl.Execute(os.Stdout, p)
if err != nil { panic(err) }
I have no errors, but the Delimiters are not changed.
tmpl, err := template.ParseFiles("base.tmpl", "homepage/inner.tmpl")
t := tmpl.Lookup("base.tmpl").Delims("[[", "]]")
p := new(Page)
err = t.Execute(os.Stdout, p)
if err != nil { panic(err) }
This leads to the same result.
In case this is relevant, my need is to embed a small angular app in a particular page of my site.
Also, I have a base template with a common HTML structure that I combine with a page-specific template with ParseFiles(), leading to this layout :
/templates/base.tmpl
/templates/homepage/inner.tmpl
/templates/otherpage/inner.tmpl
Is this possible at all ? If so, what am I doing wrong ?
Create a dummy template, set the delimiters and then parse the files:
tmpl, err := template.New("").Delims("[[", "]]").ParseFiles("base.tmpl", "homepage/inner.tmpl")
This aspect of the API is quirky and not very obvious. The API made more sense in the early days when the template package had the additional Set type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With