当前位置:网站首页>gin 模版
gin 模版
2022-07-29 06:20:00 【Mar丶流年】
模版后缀
后缀没有强制要求,起什么都可以。通常建议使用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>
//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")
}
加载多个模版
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/**/*")
模版撞名处理
假设我载入 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")
}
我们可以通过在模板中,给模板起别名解决这个问题
{ {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}}
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")
}
模版静态文件处理
模版中静态文件路径可以设置一个标识代替。在使用gin提供的Static(“标识”,“路径”)。gin 渲染模版时会将标识替换成路径地址
static/css/common.css
body {
color: red;
font-size:100px;
background: blue;
}
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}}
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")
}
边栏推荐
- Junda technology | applicable to "riyueyuan" brand ups wechat cloud monitoring card
- Flink real-time warehouse DWD layer (order placing multiple tables to realize join operation) template code
- 王树尧老师运筹学课程笔记 00 写在前面
- Summary of 2022 SQL classic interview questions (with analysis)
- 城市花样精~侬好!DESIGN#可视化电台即将开播
- Unity免费元素特效推荐
- 【charles日常问题】开启charles,使用不了钉钉
- JVM之垃圾回收机制(GC)
- 线程 - 线程安全 - 线程优化
- 王树尧老师运筹学课程笔记 04 线性代数基础
猜你喜欢
随机推荐
基于C语言设计的学籍管理系统
buck电路boot和ph引脚实测
Apisik health check test
pytest合集(7)— 参数化
模拟卷Leetcode【普通】172. 阶乘后的零
数组的子集能否累加出K
实战!聊聊如何解决MySQL深分页问题
分享一些你代码更好的小建议,流畅编码提搞效率
模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
MySQL queries are case sensitive
Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
如何优雅的写 Controller 层代码?
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
Teacher wangshuyao's notes on operations research 03 KKT theorem
Unity exploration plot access design analysis & process + code specific implementation
'function VTable for error: undefined reference to... 'cause and solution of the problem
MutationObserver文档学习
图像加噪声与矩阵求逆
Simulation volume leetcode [general] 150. evaluation of inverse Polish expression
The core of openresty and cosocket









