前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gin框架之HTML模板熏染(多模板)

gin框架之HTML模板熏染(多模板)

作者头像
大话swift
发布2020-03-12 18:24:30
5.6K0
发布2020-03-12 18:24:30
举报
文章被收录于专栏:大话swift大话swift

这篇文章我们讲讲gin的多模板渲染

首先我们要引入github.co/gin-contrib/multitemplate库

  • package main import ( "fmt" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" "html" "html/template" "net/http" "path/filepath" "strings") var engine *gin.Engine func loadTemplates(templatesDir string, module ...string) multitemplate.Renderer { r := multitemplate.NewRenderer() for _, moduleName := range module { layouts, err := filepath.Glob("./" + templatesDir + "/" + moduleName + "/*.html") if err != nil { panic(err.Error()) } includes, err := filepath.Glob("./" + templatesDir + "/" + moduleName + "/**/*.html") if err != nil { panic(err.Error()) } for _, include := range includes { fmt.Println(layouts) layoutCopy := make([]string, len(layouts)) copy(layoutCopy, layouts) files := append(layoutCopy, include) //给模板添加自定义函数 r.AddFromFilesFuncs( strings.Replace(filepath.ToSlash(include), templatesDir, "", -1), template.FuncMap{ "xss": xss, "adminUrl": adminUrl, "url": url, "myFunc": myFunc, }, files...) } } return r} func xss(a string) string { return html.EscapeString(a)} func adminUrl(path string) string { return "/admin/" + strings.Trim(path, "/")}func url(path string) string { return "/admin/" + strings.Trim(path, "/")}func myFunc(s string) string { return s + "myFunc"}func init() { engine = gin.Default() //设置模板渲染 engine.HTMLRender = loadTemplates("templates", "web", "admin")} // router routerfunc router() *gin.Engine { return engine} func main() { router := router() router.GET("/a", func(c *gin.Context) { //加载名称为/web/a/index.html的模板,并给模板赋值 c.HTML(http.StatusOK, "/web/a/index.html", gin.H{ "title": "gin框架之HTML模板渲染-a", }) }) router.GET("/b", func(c *gin.Context) { //加载名称为/web/b/index.html,并给模板赋值 c.HTML(http.StatusOK, "/web/b/index.html", gin.H{ "title": "gin框架之HTML模板渲染-b", }) }) router.GET("/func", func(c *gin.Context) { //加载名称为/web/func/index.html,并给模板赋值 c.HTML(http.StatusOK, "/web/func/index.html", gin.H{ "title": "gin框架之HTML模板渲染- ", }) }) router.Run(":8080")}

‍1、更改gin的模板渲染

代码语言:javascript
复制
func init() {  engine = gin.Default()  //设置模板渲染  engine.HTMLRender = loadTemplates("templates", "web", "admin")}

2、加载模板

代码语言:javascript
复制
func loadTemplates(templatesDir string, module ...string) multitemplate.Renderer {  r := multitemplate.NewRenderer()  for _, moduleName := range module {    layouts, err := filepath.Glob("./" + templatesDir + "/" + moduleName + "/*.html")    if err != nil {      panic(err.Error())    }
    includes, err := filepath.Glob("./" + templatesDir + "/" + moduleName + "/**/*.html")    if err != nil {      panic(err.Error())    }    for _, include := range includes {      fmt.Println(layouts)      layoutCopy := make([]string, len(layouts))      copy(layoutCopy, layouts)      files := append(layoutCopy, include)      //给模板添加自定义函数      r.AddFromFilesFuncs(        strings.Replace(filepath.ToSlash(include), templatesDir, "", -1),        template.FuncMap{          "xss":      xss,          "adminUrl": adminUrl,          "url":      url,          "myFunc":   myFunc,        }, files...)    }
  }
  return r}

这个函数,把templates目录下的模板按照文件路径的方式进行命名,例如/web/a/index.html,这个就是一个模板的名称。简单的说就是把模板的名称和文件路径进行映射(我没有进行递归遍历)。

3、给模板添加自定义函数

代码语言:javascript
复制
      r.AddFromFilesFuncs(        strings.Replace(filepath.ToSlash(include), templatesDir, "", -1),        template.FuncMap{          "xss":      xss,          "adminUrl": adminUrl,          "url":      url,          "myFunc":   myFunc,        }, files...)有兴趣的同学可以去百度云获取代码
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话swift 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档