我是 Golang 世界的新手,我正在尝试建立一个带有模板文件和良好缓存系统的 Web 项目。
layout.html1.html2.html
layout.html
err := templates.ExecuteTemplate(w, "layout.html", nil)
layout.html
...
<body>{{template "content" .}}</body>
...
1.html
{{define "content"}}This is the first page.{{end}}
2.html
{{define "content"}}This is the second page.{{end}}
我不会用
var templates = template.Must(template.ParseFiles(
"layout.html",
"1.html",
"2.html"))
2.html1.html
所以我有两种方法:
templates["1"] = template.Must(template.ParseFiles("layout.html","1.html"))
templates["2"] = template.Must(template.ParseFiles("layout.html","2.html"))
有没有新的或更好的方法来做到这一点?