From 6751afdfdecccba525c07514d42b5f20d2c70e53 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 26 Feb 2019 17:02:43 +0800 Subject: [PATCH] add template example --- routes/routes.go | 18 ++++++++++++++++++ templates/content.tmpl | 3 +++ templates/foot.tmpl | 3 +++ templates/head.tmpl | 7 +++++++ 4 files changed, 31 insertions(+) create mode 100644 templates/content.tmpl create mode 100644 templates/foot.tmpl create mode 100644 templates/head.tmpl diff --git a/routes/routes.go b/routes/routes.go index f6c1a49..ccf86cf 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,7 +1,10 @@ package routes import ( + "io/ioutil" + "log" "net/http" + "path" "git.trj.tw/golang/go-file-serve/module/context" "git.trj.tw/golang/go-file-serve/routes/api" @@ -19,12 +22,27 @@ func NewEngine() *gin.Engine { // SetRoutes - func SetRoutes(r *gin.Engine) { + tmpls := make([]string, 0) + files, err := ioutil.ReadDir("./templates") + if err != nil { + log.Fatal(err) + } + for _, v := range files { + if !v.IsDir() { + tmpls = append(tmpls, path.Join("./templates", v.Name())) + } + } + r.LoadHTMLFiles(tmpls...) r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "message": "success", }) }) + r.GET("/html", func(c *gin.Context) { + c.HTML(200, "content.tmpl", gin.H{"title": "test title"}) + }) + apiGroup := r.Group("/api") { apiGroup.GET("/files", context.PatchContext(api.GetFileList)) diff --git a/templates/content.tmpl b/templates/content.tmpl new file mode 100644 index 0000000..d3a5aea --- /dev/null +++ b/templates/content.tmpl @@ -0,0 +1,3 @@ +{{ template "head.tmpl" . }} +Content +{{ template "foot.tmpl" . }} diff --git a/templates/foot.tmpl b/templates/foot.tmpl new file mode 100644 index 0000000..69aae56 --- /dev/null +++ b/templates/foot.tmpl @@ -0,0 +1,3 @@ + + + diff --git a/templates/head.tmpl b/templates/head.tmpl new file mode 100644 index 0000000..8f66eed --- /dev/null +++ b/templates/head.tmpl @@ -0,0 +1,7 @@ + + + + + {{ .title }} + +