我是靠谱客的博主 怕孤单画板,这篇文章主要介绍GO_GIN_不同文件下html模版渲染,现在分享给大家,希望可以做个参考。

  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
	- 
复制代码

最后

以上就是怕孤单画板最近收集整理的关于GO_GIN_不同文件下html模版渲染的全部内容,更多相关GO_GIN_不同文件下html模版渲染内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部