add template example

This commit is contained in:
Jay 2019-02-26 17:02:43 +08:00
parent 155e41f618
commit 6751afdfde
4 changed files with 31 additions and 0 deletions

View File

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

3
templates/content.tmpl Normal file
View File

@ -0,0 +1,3 @@
{{ template "head.tmpl" . }}
Content
{{ template "foot.tmpl" . }}

3
templates/foot.tmpl Normal file
View File

@ -0,0 +1,3 @@
</body>
</html>

7
templates/head.tmpl Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ .title }}</title>
</head>
<body>