One layout template with three children templates.
layout.html
<html>
<body>
{{template "tags"}}
{{template "content"}}
{{template "comment"}}
</body>
</html>
tags.html
{{define "tags"}}
<div>
{{.Name}}
<div>
{{end}}
content.html
{{define "content"}}
<div>
<p>{{.Title}}</p>
<p>{{.Content}}</p>
</div>
{{end}}
comment.html
{{define "tags"}}
<div>
{{.Note}}
</div>
{{end}}
gocode
type Tags struct {
Id int
Name string
}
type Content struct {
Id int
Title string
Content string
}
type Comment struct {
Id int
Note string
}
func main() {
tags := &Tags{"Id":1, "Name":"golang"}
Content := &Content{"Id":9, "Title":"Hello", "Content":"World!"}
Comment := &Comment{"Id":2, "Note":"Good Day!"}
}
I am confused that how to render each children template and combine the result to layout output.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…