如何使用golang html / template的基本模板文件?
发布时间:2020-12-16 09:22:16 所属栏目:大数据 来源:网络整理
导读:有gin-gonic网络应用程序. 有3个文件: 1)base.html – 基本布局文件 !DOCTYPE htmlhtml lang="en"bodyheader...{{template "content" .}}footer.../body/html 2)page1.html,用于/ page1 {{define "content"}}div h1Page1/h1/div{{end}}{{template "base.htm
有gin-gonic网络应用程序.
有3个文件: 1)base.html – 基本布局文件 <!DOCTYPE html> <html lang="en"> <body> header... {{template "content" .}} footer... </body> </html> 2)page1.html,用于/ page1 {{define "content"}} <div> <h1>Page1</h1> </div> {{end}} {{template "base.html"}} 3)page2.html,用于/ page2 {{define "content"}} <div> <h1>Page2</h1> </div> {{end}} {{template "base.html"}} 问题是/ page1和/ page2使用一个模板 – page2.html.我认为我对这种结构有误解:{{define“content”}},{{template“base.html”}}. 请问,您能举例说明如何在golang中使用基本布局吗? 解决方法
只要您将模板与“内容”一起解析,就可以使用base.html,如下所示:
base.html文件 {{define "base"}} <!DOCTYPE html> <html lang="en"> <body> header... {{template "content" .}} footer... </body> </html> {{end}} page1.html {{define "content"}} I'm page 1 {{end}} page2.html {{define "content"}} I'm page 2 {{end}} 然后ParseFiles与(“your-page.html”,“base.html”)和ExecuteTemplate与您的上下文. tmpl,err := template.New("").ParseFiles("page1.html","base.html") // check your err err = tmpl.ExecuteTemplate(w,"base",yourContext) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |