I want to know what is the true way to send any data to template (the html/template package)? My code is below:
var templates = template.Must(template.ParseFiles(
path.Join(this.currentDirectory, "views/base.html"),
path.Join(this.currentDirectory, "views/main/test.html"),
))
templates.Execute(response, map[string]string{
"Variable": "Тест!",
})
And that's template:
{{define "content"}}
{{ .Variable }}
{{end}}
I will be thankful!
Your template has a name, "content"
, so you need to specifically execute that template.
templates.ExecuteTemplate(os.Stdout, "content", map[string]string{
"Variable": "Тест!",
})
You may not be parsing what you think. From the template.ParseFiles
documentation (emphasis mine)
The returned template's name will have the (base) name and (parsed) contents of the first file
Try using:
t, err := template.New("base").ParseFiles("base.html", "test.html")
if err != nil { ... }
t.Execute(response, variables)
And here's a playground example if it helps.