一个Golang 项目的模版文件结构参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|-- app.yaml
|-- app
| +-- http.go
|-- templates
| +-- base.html
+-- github.com
+-- storeski
+-- appengine
|-- products
| |-- http.go
| +-- templates
| |-- list.html
| +-- detail.html
+-- account
|-- http.go
+-- templates
|-- overview.html
+-- notifications.html
base.html
1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE HTML>
<html>
<head>
<title>{{.Store.Title}}</title>
</head>
<body>
<div id="content">
{{template "content" .}}
</div>
</body>
</html>
products/templates/list.html
1
2
3
{{define "content"}}
<h1> Products List </h1>
{{end}}
products/http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func init() {
http.HandleFunc("/products", listHandler)
}
var listTmpl = template.Must(template.ParseFiles(
"templates/base.html",
"/appengine/products/templates/list.html" ))
func listHandler(w http.ResponseWriter, r *http.Request) {
tc := make(map[string]interface{})
tc["Store"] = Store
tc["Products"] = Products
if err := listTmpl.Execute(w, tc); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
本文网址: https://golangnote.com/topic/21.html 转摘请注明来源