When I run:
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
the page loads fine. But when I try and run
t := template.New("first")
t, _ = t.ParseFiles("index.html")
t.Execute(w, nil)
the only thing that loads is a blank page. I am trying to change the delimiter values in a Golang html template and would like to make the template, change the delimiter values, then parse the file.
Does anyone else have this problem?
The first version works as you expect because the package-level ParseFiles
function will return a new template that has the name and content of the first parsed file.
In the second case, though, you're creating a template named "first"
and then parsing one with name "index.html"
. When you call t.Execute
on "first"
, it's still empty.
You can fix the problem by either:
template.New("index.html")
, so that the file name matches the template name you parse next;t.ExecuteTemplate(w, "index.html", nil)
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