golang template - how to render templates?

匿名 (未验证) 提交于 2019-12-03 01:11:01

问题:

One layout template with three children templates.

layout.html

        {{template "tags"}}      {{template "content"}}      {{template "comment"}}     

tags.html

{{define "tags"}} 
{{.Name}}
{{end}}

content.html

{{define "content"}} 

{{.Title}}

{{.Content}}

{{end}}

comment.html

{{define "tags"}} 
{{.Note}}
{{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.

回答1:

As always, the doc is a good place to start.

I wrote a working example on the playground

To explain a bit:

  1. You don't need strings in struct literals: &Tags{Id: 1}, not &Tags{"Id":1}
  2. You can only pass a single object to your template to execute, which will dispatch objects to each subtemplate as you require in the {{template }} instruction. I used a ad-hoc Page struct, but a map[string]interface{} would do if you prefer.
  3. You need to parse each template (I used strings in the Playground, but ParseFiles would do if you have your html files already)
  4. I used os.Stdout to execute it, but you should obviously replace that by the corresponding ResponseWriter

And the whole code:

package main  import "fmt" import "html/template" import "os"  var page = `        {{template "tags" .Tags}}      {{template "content" .Content}}      {{template "comment" .Comment}}    `  var tags = `{{define "tags"}} 
{{.Name}}
{{end}}` var content = `{{define "content"}}

{{.Title}}

{{.Content}}

{{end}}` var comment = `{{define "comment"}}
{{.Note}}
{{end}}` type Tags struct { Id int Name string } type Content struct { Id int Title string Content string } type Comment struct { Id int Note string } type Page struct { Tags *Tags Content *Content Comment *Comment } func main() { pagedata := &Page{Tags:&Tags{Id:1, Name:"golang"}, Content: &Content{Id:9, Title:"Hello", Content:"World!"}, Comment: &Comment{Id:2, Note:"Good Day!"}} tmpl := template.New("page") var err error if tmpl, err = tmpl.Parse(page); err != nil { fmt.Println(err) } if tmpl, err = tmpl.Parse(tags); err != nil { fmt.Println(err) } if tmpl, err = tmpl.Parse(comment); err != nil { fmt.Println(err) } if tmpl, err = tmpl.Parse(content); err != nil { fmt.Println(err) } tmpl.Execute(os.Stdout, pagedata) }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!