我是靠谱客的博主 拼搏枕头,这篇文章主要介绍Gin html模板引用,现在分享给大家,希望可以做个参考。

提取公共部分

1.header.html

复制代码
1
2
3
4
5
6
7
8
9
10
{{define "header"}} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span>ddddd</span> {{end}}

2.footer.html

复制代码
1
2
3
4
5
6
7
{{define "footer"}} <span>end</span> </body> </html> {{end}}

注意:define部分只写文件的名字,不加路径和后缀名

使用不同路径下但相同文件名的模板
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func main() { router := gin.Default() router.LoadHTMLGlob("views/**/*") router.GET("/main/newlist", func(c *gin.Context) { c.HTML(http.StatusOK, "main/newlist.html",gin.H{ "title": "Posts", }) }) router.GET("/users/index", func(c *gin.Context) { c.HTML(http.StatusOK, "users/index.tmpl",gin.H{ "title": "Users", }) }) router.Run(":8080") }

其上代码分别引用了views文件夹下的posts文件夹和users文件夹。在HTML文件中的写发如下:

复制代码
1
2
3
4
5
6
7
{{define "main/newlist.html"}} {{template "header" .}} <div>middle</div> {{template "footer" }} {{end}}

注意:在define中HTML文件的路径和后缀名都必须加上,跟引用公共模板正好相反。

最后

以上就是拼搏枕头最近收集整理的关于Gin html模板引用的全部内容,更多相关Gin内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部