GolangNote

Golang笔记

一个Golang 项目的模版文件结构参考

Permalink

一个Golang 项目的模版文件结构参考

Golang 项目的模版文件结构

plaintext: layout
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

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

HTML: list.html
1
2
3
{{define "content"}}
  <h1> Products List </h1>
{{end}}

products/http.go

Go: http handle
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)
  }
}

Related articles

谷歌翻译的 golang 库推荐

Google 的翻译越来越好了,官方的Golang SDK 已经很完美,这里介绍的是几个非官方发布的有特色的库。...

Write a Comment to "一个Golang 项目的模版文件结构参考"

Submit Comment Login
Based on Golang + fastHTTP + youdb | go1.16 Processed in 1ms