dragon8002 2013-10-23 16:11
浏览 22
已采纳

Golang模板-如何呈现模板?

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.

  • 写回答

1条回答 默认 最新

  • drot98385 2013-10-23 17:08
    关注

    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 <name> <arg>}} 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 = `<html>
      <body>
        {{template "tags" .Tags}}
    
        {{template "content" .Content}}
    
        {{template "comment" .Comment}}
      </body>
    </html>`
    
    var tags = `{{define "tags"}}
    <div>
        {{.Name}}
    <div>
    {{end}}`
    
    var content = `{{define "content"}}
    <div>
       <p>{{.Title}}</p>
       <p>{{.Content}}</p>
    </div>
    {{end}}`
    
    var comment = `{{define "comment"}}
    <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
    }
    
    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路