Golang html / templates: ParseFiles with custom Delims

Using delimited templates works great when used template.New("...").Delims("[[", "]]").Parse()

However, I cannot figure out how to get the same result withtemplate.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 don't change.

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 it matters, I need to embed a small angular app on a specific page of my site.

Also, I have a basic template with general HTML structure that I combine with a template for a page with ParseFiles (), resulting in this layout:

/templates/base.tmpl
/templates/homepage/inner.tmpl
/templates/otherpage/inner.tmpl

      

Is this even possible? If so, what am I doing wrong?

+3


source to share


1 answer


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 an additional Set type

+5


source







All Articles