I need help. I need to use "html/template"
's marking ({{.Title}}
, example) in sub-files("article.html"
, example in my text):
// ...
type Page struct {
Test string
}
type News struct {
Page
Title string
}
func main() {
t, _ := template.ParseFiles(filepath+"core.tmpl", filepath+"article.tmpl")
p := &News{
Title: "TITLE",
Page: Page{
Test: "TITLE",
},
}
t.Execute(wr, p)
}
Code in core.tmpl
:
{{template "article"}}
Code in article.tmpl
:
{{define "article"}}
{{.Title}}<br><br>
{{.Page.Test}}
{{end}}
In your core.tmpl
you have to use
{{template "article" .}}
If you don't specify the .
at the end, the template will be executed with nil
data. Specifying the .
will pass the value of .
to the invoked template.
Quoting from the text/template
package documentation, Actions
section:
{{template "name"}}
The template with the specified name is executed with nil data.
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.