golang template用法很简单:
tpl, _ := template.ParseFiles("templates/post.html")
tpl.Execute(w, nil)
{{define "layout"}}
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{template "body" .}}
</body>
</html>
{{end}}
{{template "layout" .}}
{{define "body"}}
<div>
this is body
</div>
{{end}}
tpl, _ := template.ParseFiles("templates/post.html", "templates/layout.html")
tpl.Execute(w, nil)
var funcMaps = template.FuncMap{
"empty": func(str string) bool {
if str == "" {
return true
} else {
return false
}
},
}
tpl, err := template.New("post.html").Funcs(funcMaps).ParseFiles("templates/post.html", "templates/layout.html")
if err != nil {
//...
}
tpl.Execute(w, nil)