GO_GIN_不同文件下html模版渲染

2,124 阅读1分钟
  1. 路由编写
	// 注册一个默认的路由器
	router := gin.Default()
	// 加载static文件夹下所有的文件
	router.LoadHTMLGlob("com.zy/static/templates/**/*")
	"templates/template2.templates")
	router.GET("users/login", func(c *gin.Context) {
		// 注意下面将gin.H参数传入index.tmpl中!也就是使用的是index.tmpl模板
		c.HTML(http.StatusOK, "users/login.html", gin.H{
			"title": "GIN: 测试加载HTML模板",
		})
	})
	router.GET("posts/login", func(c *gin.Context) {
		// 注意下面将gin.H参数传入index.tmpl中!也就是使用的是index.tmpl模板
		c.HTML(http.StatusOK, "posts/login.html", gin.H{
			"title": "GIN: 测试加载HTML模板",
		})
	})
  1. posts/login.html
    开头:{{ define "posts/login.html" }}
    #"posts/login.html" 对应 c.HTML(http.StatusOK, "posts/login.html", gin.H{...})
    结尾:{{ end }}
    {{ define "posts/login.html" }}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        login1
    </body>
    </html>
    {{end}}
  1. 控制台

[GIN-debug] Loaded HTML Templates (4): 
	- 
	- login.html
	- posts/login.html
	- users/login.html
	-