import "os"
import "html/template"
...
t, _ := template.ParseFiles("login.html")
t.Execute(os.Stdout, data)
...
login.html:
{{ template "header.html" . }}
<form ....>...</form>
{{ template "footer.html" . }}
no output, no error.
If I remove those two lines of {{ template "..." . }}, I could see the part being outputed.
What's required to make {{ template "..." . }} work or am I misunderstanding html/template completely?
You need to define a name for the file that will contain the other templates and then execute that.
login.tmpl
{{define "login"}}
<!doctype html>
<html lang="en">
..
{{template "header" .}}
</body>
</html>
{{end}}
header.tmpl
{{define "header"}}
whatever
{{end}}
Then you parse both those files
t := template.Must(template.ParseFiles("login.tmpl", "header.tmpl"))
// and then execute the template with the defined name
t.ExecuteTemplate(os.Stdout, "login", data)
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