golang模板 - 如何呈现模板? [英] golang template - how to render templates?
本文介绍了golang模板 - 如何呈现模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
layout.html
< HTML>
< body>
{{templatetags}}
{{templatecontent}}
{{templatecomment}}
< /体>
< / html>
tags.html
{{definetags}}
< div>
{{.Name}}
< div>
{{end}}
content.html
{{definecontent}}
< div>
< p> {{。标题}}< / p>
< p> {{。Content}}< / p>
< / div>
{{end}}
comment.html
{{definetags}}
< div>
{{.Note}}
< / div>
{{end}}
gocode
类型标签struct {
Id int
名称字符串
}
类型内容struct {
Id int
标题字符串
内容字符串
}
类型注释struct {
Id int
注意字符串
}
func main(){
tags:=& Tags {Id:1,Name:golang}
Content:=& Content {Id:9,Title:Hello,Content:World!}
评论:=& Comment {Id:2,Note:Good Day! }
}
我很困惑,如何渲染每个子模板并将结果布局输出。
感谢。 与往常一样,文档是一个很好的开始。
解释一下:
- 结构文字中不需要字符串:
& Tags {Id:1}
,而不是& Tags {Id:1}
- 您只能将一个对象传递给您的模板来执行,这将根据您在
{{template< name> < arg>}}
指令。我使用了特殊的页面
结构,但是如果您愿意,可以使用map [string] interface {}
。 - 您需要解析每个模板(我在Playground中使用了字符串,但 ParseFiles 会在您已经有html文件的情况下执行)
- 我使用os.Stdout来执行它,但是您显然应该用相应的
ResponseWriter
以及整个代码:
包主
$ b导入fmt
导入html / template
导入os
var page =`< html>
< body>
{{templatetags.Tags}}
{{templatecontent.Content}}
{{templatecomment.Comment}}
< / body>
< / html>`
var tags =`{{definetags}}
< div>
{{.Name}}
< div>
{{end}}`
var content =`{{definecontent}}
< div>
< p> {{。标题}}< / p>
< p> {{。Content}}< / p>
< / div>
{{end}}`
var comment =`{{definecomment}}
< div>
{{.Note}}
< / div>
{{end}}`
类型标签struct {
Id int
名称字符串
}
类型内容struct {
Id int
标题字符串
内容字符串
}
类型注释struct {
Id int
注意字符串
类型页面结构{
标签*标签
内容*内容
评论*评论
}
func main(){
pagedata:=& Page {标签:&标签{Id:1,Name:golang},
Content:& Content {Id:9,Title: ,Content:World!},
评论:& Comment {Id:2,注意:Good Day!}}
tmpl:= template.New(page)
var err错误
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)
}
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.
解决方案
As always, the doc is a good place to start.
I wrote a working example on the playground
To explain a bit:
- You don't need strings in struct literals:
&Tags{Id: 1}
, not&Tags{"Id":1}
- 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-hocPage
struct, but amap[string]interface{}
would do if you prefer. - You need to parse each template (I used strings in the Playground, but ParseFiles would do if you have your html files already)
- 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)
}
这篇关于golang模板 - 如何呈现模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文