相关疑难解决方法(0)

golang模板 - 如何呈现模板?

一个带有三个子模板的布局模板.

的layout.html

<html>
  <body>
    {{template "tags"}}

    {{template "content"}}

    {{template "comment"}}
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

tags.html

{{define "tags"}}
<div>
    {{.Name}}
<div>
{{end}}
Run Code Online (Sandbox Code Playgroud)

content.html

{{define "content"}}
<div>
   <p>{{.Title}}</p>
   <p>{{.Content}}</p>
</div>
{{end}}
Run Code Online (Sandbox Code Playgroud)

comment.html

{{define "tags"}}
<div>
    {{.Note}}
</div>
{{end}}
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

go

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

go ×1