当前位置:网站首页>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")
}
边栏推荐
- 实现改变一段文字的部分颜色效果
- vim文本编辑器的一些使用小技巧
- Can MySQL export tables regularly?
- MySQL queries are case sensitive
- MutationObserver文档学习
- Talk about tcp/ip protocol? And the role of each layer?
- [C language brush leetcode] 1054. Bar code with equal distance (m)
- Cesium反射
- Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
- 我的创业邻居们
猜你喜欢

Unity探索地块通路设计分析 & 流程+代码具体实现

Thread synchronization - producers and consumers, tortoise and rabbit race, dual thread printing

MutationObserver文档学习

IDEA找不到Database解决方法

MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of

Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)

buck电路boot和ph引脚实测

Unity免费元素特效推荐

Unity exploration plot access design analysis & process + code specific implementation

图像加噪声与矩阵求逆
随机推荐
vscode通过remotessh结合xdebug远程调试php解决方案
Teacher Wu Enda's machine learning course notes 02 univariate linear regression
leetcode-1331:数组序号转换
DM数据守护集群搭建
Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
如何优雅的写 Controller 层代码?
mysql可以定时导出表格吗?
Share some tips for better code, smooth coding and improve efficiency
Flink实时仓库-DWD层(流量域)模板代码
Cesium reflection
The difference between pairs and ipairs
pytorch的技巧记录
MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of
Teacher Cui Xueting's course notes on optimization theory and methods 00 are written in the front
[CF1054H] Epic Convolution——数论,卷积,任意模数NTT
王树尧老师运筹学课程笔记 06 线性规划与单纯形法(几何意义)
Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
IDEA找不到Database解决方法
谷歌零碎笔记之JWT(草稿)
建木持续集成平台v2.5.2发布