gin中的多模板和模板继承的用法

1. 简单用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
 
import (
    "github.com/gin-contrib/multitemplate"
    "github.com/gin-gonic/gin"
    "path/filepath"
)
 
func createMyRender() multitemplate.Renderer {
    r := multitemplate.NewRenderer()
    basePath, _ := filepath.Abs("templates")
    r.AddFromFiles("index", basePath + `\base.html`, basePath + `\index.html`)
    r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html")
    return r
}
 
func main() {
    router := gin.Default()
    router.HTMLRender = createMyRender()
    router.GET("/", func(c *gin.Context) {
        c.HTML(200, "index", gin.H{
            "title": "Html5 Template Engine",
        })
    })
    router.GET("/article", func(c *gin.Context) {
        c.HTML(200, "article", gin.H{
            "title": "Html5 Article Engine",
        })
    })
    router.GET("/json", func(context *gin.Context) {
        context.String(200, "OK")
    })
 
    router.Run()
}

  

 

 

 

2. 高级用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
 
import (
    "github.com/gin-contrib/multitemplate"
    "github.com/gin-gonic/gin"
    "path/filepath"
)
 
func main() {
    router := gin.Default()
    router.HTMLRender = loadTemplates("./templates")
    router.GET("/", func(c *gin.Context) {
        c.HTML(200, "index.html", gin.H{
            "title": "Welcome!",
        })
    })
    router.GET("/article", func(c *gin.Context) {
        c.HTML(200, "article.html", gin.H{
            "title": "Html5 Article Engine",
        })
    })
    router.GET("/login", func(context *gin.Context) {
        context.HTML(200, "login.html", gin.H{
            "title": "Login",
        })
    })
 
    router.Run()
}
 
func loadTemplates(templatesDir string) multitemplate.Renderer {
    r := multitemplate.NewRenderer()
 
    adminLayouts, err := filepath.Glob(templatesDir + "/layouts/base_admins.html")
    if err != nil {
        panic(err.Error())
    }
    adminInclude, err := filepath.Glob(templatesDir + "/admins/*.html")
    if err != nil {
        panic(err.Error())
    }
    // Generate our templates map from our layouts/ and articles/ directories
    for _, include := range adminInclude {
        layoutCopy := make([]string, len(adminLayouts))
        copy(layoutCopy, adminLayouts)
        files := append(layoutCopy, include)
        r.AddFromFiles(filepath.Base(include), files...)
    }
 
    articleLayouts, err := filepath.Glob(templatesDir + "/layouts/base_articles.html")
    if err != nil {
        panic(err.Error())
    }
    articleInclude, err := filepath.Glob(templatesDir + "/articles/*.html")
    if err != nil {
        panic(err.Error())
    }
    // Generate our templates map from our layouts/ and articles/ directories
    for _, include := range articleInclude {
        layoutCopy := make([]string, len(articleLayouts))
        copy(layoutCopy, articleLayouts)
        files := append(layoutCopy, include)
        r.AddFromFiles(filepath.Base(include), files...)
    }
 
    return r
}

  

项目结构

 

posted @   专职  阅读(1287)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示