• gin 模版


    模版后缀

    后缀没有强制要求,起什么都可以。通常建议使用tmpl作为后缀

    模版渲染

    使用 LoadHTMLFiles(模版路径…) 加载模版
    使用 gin.Context HTML(状态码,模版名称,模版参数) 渲染指定模版模版
    参数渲染使用 {{.变量名}}

    ///Users/xieruixiang/go/src/ServiceProxy/public/template/user.tmpl
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    {{.title}}
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    //Users/xieruixiang/go/src/ServiceProxy/public/main.go
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    	"path/filepath"
    	"runtime"
    )
    
    var basePath string
    
    func init() {
        //获取当前main.go 位置
    	_, file, _, _ := runtime.Caller(0)
    	basePath = filepath.Dir(file)
    }
       
    func getPath(path string) string {
        //拼接以main.go 为启始位置的绝对路径
    	return filepath.Join(basePath, path)
    }
    
    func main() {
    	r := gin.Default()
        //加载user.tmpl模版
        //这里使用绝对路径,是为了兼容在goland中运行
        //goland编辑器是在临时目录中运行,临时目录中没有模板,所以相对路径找不到
        //如果是正常的在 Users/xieruixiang/go/src/ServiceProxy/public 下 go run main.go 填写相对路径是没有问题的
    	r.LoadHTMLFiles(getPath("template/user.tmpl"))
    	r.GET("/user", func(c *gin.Context) {
            //渲染user.html模版,指定模版参数
    		c.HTML(http.StatusOK, "user.tmpl", gin.H{
    			"title": "用户列表",
    		})
    	})
    	r.Run(":8080")
    }
    
    • 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

    加载多个模版

    LoadHTMLFiles() 是支持加载多个模版的,但是模版数量很多,你不会一个一个敲进去吧:LoadHTMLFiles(tmeplate1,template2…template100)
    这时候可以使用 LoadHTMLGlob(pattern string) 载入,pattern中以两个星代表目录,一个星代表文件

    	//加载template目录下所有文件,如:template/user.tmpl
    	LoadHTMLGlob("template/*")
    	//加载template下一级目录中的所有文件,如:template/a/user.tmpl,但不会加载template下的文件(如:template/good.tmpl)
    	LoadHTMLGlob("template/**/*")
    
    • 1
    • 2
    • 3
    • 4

    模版撞名处理

    假设我载入 template/b/user.tmpl和template/a/user.tmpl
    通过HTML()指定user.tmpl时会使用后载入的模版。使用a/user.tmpl作为模版名也是无效的

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    	"path/filepath"
    	"runtime"
    )
    
    var basePath string
    
    func init() {
    	_, file, _, _ := runtime.Caller(0)
    	basePath = filepath.Dir(file)
    }
    
    func getPath(path string) string {
    	return filepath.Join(basePath, path)
    }
    
    func main() {
    	r := gin.Default()
    
    	r.LoadHTMLGlob(getPath("template/**/*"))
    
    
    	r.GET("/user", func(c *gin.Context) {
    		//使用的是后载入的 b/user.tmpl 模板
    		c.HTML(http.StatusOK, "user.tmpl", gin.H{
    			"title": "用户列表",
    		})
    	})
    	r.Run(":8080")
    }
    
    
    • 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

    我们可以通过在模板中,给模板起别名解决这个问题
    {{define “别名”}} html content {{end}}

    //a/user.tmpl
    {{define "a/user"}}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    this is a user.tmpl
    {{.title}}
    </body>
    </html>
    {{end}}
    
    //b/user.tmpl
    {{define "b/user"}}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    {{.title}}
    </body>
    </html>
    {{end}}
    
    • 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
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    	"path/filepath"
    	"runtime"
    )
    
    var basePath string
    
    func init() {
    	_, file, _, _ := runtime.Caller(0)
    	basePath = filepath.Dir(file)
    }
    
    func getPath(path string) string {
    	return filepath.Join(basePath, path)
    }
    
    func main() {
    	r := gin.Default()
    
    
    	r.LoadHTMLGlob(getPath("template/**/*"))
    
    	r.GET("/user", func(c *gin.Context) {
            //a/user.tmpl
    		c.HTML(http.StatusOK, "a/user", gin.H{
    			"title": "用户列表",
    		})
    	})
    
    	r.GET("/b/user", func(c *gin.Context) {
    		//b/user.tmpl
    		c.HTML(http.StatusOK, "b/user", gin.H{
    			"title": "用户列表",
    		})
    	})
    
    
    	r.Run(":8080")
    }
    
    
    • 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

    模版静态文件处理

    模版中静态文件路径可以设置一个标识代替。在使用gin提供的Static(“标识”,“路径”)。gin 渲染模版时会将标识替换成路径地址

    static/css/common.css

    body {
        color: red;
        font-size:100px;
        background: blue;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    template/a/user.tmpl

    {{define "a/user"}}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <link rel="stylesheet" type="text/css" href="static/css/common.css"/>
    <body>
    this is a user.tmpl
    {{.title}}
    </body>
    </html>
    {{end}}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    	"path/filepath"
    	"runtime"
    )
    
    var basePath string
    
    func init() {
    	_, file, _, _ := runtime.Caller(0)
    	basePath = filepath.Dir(file)
    }
    
    func getPath(path string) string {
    	return filepath.Join(basePath, path)
    }
    
    func main() {
    	r := gin.Default()
    
    	r.LoadHTMLGlob(getPath("template/**/*"))
    	r.Static("/static", getPath("static"))
    
    
    	r.GET("/user", func(c *gin.Context) {
    		//a/user.tmpl
    		c.HTML(http.StatusOK, "a/user", gin.H{
    			"title": "用户列表",
    		})
    	})
    
    	r.Run(":8080")
    }
    
    
    • 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
  • 相关阅读:
    【运行时数据区和程序计数器】
    EasyX图形库的下载安装与Dev-C++配置
    第十四届蓝桥杯模拟赛第一期试题与题解Java
    【C语言入门数据结构3】链表之单链表
    数据结构-快速排序
    计算机体系结构和CPU工作原理
    【算法-数组3】螺旋数组(一入循环深似海啊!)
    Spring源码解析——Spring事务是怎么通过AOP实现的?
    MySQL-MVCC多版本控制及事务的隔离性
    python下拉框选择测试
  • 原文地址:https://blog.csdn.net/qq_29744347/article/details/126028984