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)
  }
}

本文网址: https://golangnote.com/topic/21.html 转摘请注明来源

Related articles

Golang quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

golang 实现的基于web的文件管理-filebrowser

FileBrowser 在指定目录中提供了一个文件管理界面,可用于上传,删除,预览,重命名和编辑文件。它允许创建多个用户,每个用户都可以有自己的目录。它可以用作独立的应用程序。...

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

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms