一個布局模板與三個子模板。布局.html<html> <body> {{template "tags"}} {{template "content"}} {{template "comment"}} </body></html>標(biāo)簽.html{{define "tags"}}<div> {{.Name}}<div>{{end}}內(nèi)容.html{{define "content"}}<div> <p>{{.Title}}</p> <p>{{.Content}}</p></div>{{end}}評論.html{{define "tags"}}<div> {{.Note}}</div>{{end}}代碼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!"}}我很困惑如何呈現(xiàn)每個子模板并將結(jié)果組合到布局輸出。
Golang 模板 - 如何渲染模板?
慕工程0101907
2021-07-01 15:21:57