当前位置:网站首页>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")
}
边栏推荐
- Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
- IO stream - file - properties
- Teacher Wu Enda machine learning course notes 01 introduction
- SSH password free login - two virtual machines establish password free channel two-way trust
- Thread synchronization - producers and consumers, tortoise and rabbit race, dual thread printing
- Is online legend software testing training really so black hearted? Are they all scams?
- 基于C语言设计的学籍管理系统
- The difference between pairs and ipairs
- Unity exploration plot access design analysis & process + code specific implementation
- Flink实时仓库-DWD层(处理复杂数据-流和表的装换处理)模板代码
猜你喜欢

网上传说软件测试培训真的那么黑心吗?都是骗局?

JVM之垃圾回收机制(GC)

上采样之反卷积操作

Flink real-time warehouse DWD layer (transaction domain - additional purchase dimension degradation processing) template code

Flink real time warehouse DWD layer (traffic domain) template code

Cvpr2022oral special series (I): low light enhancement

Improved pillar with fine grained feature for 3D object detection paper notes

10道面试常问JVM题

Revolution of game assets

【charles日常问题】开启charles,使用不了钉钉
随机推荐
Jetpack Compose 中的键盘处理
Analog volume leetcode [normal] 093. Restore IP address
城市花样精~侬好!DESIGN#可视化电台即将开播
Image noise and matrix inversion
SS command details
Flink real-time warehouse DWD layer (processing complex data - installation and replacement of streams and tables) template code
Not so simple singleton mode
模拟卷Leetcode【普通】172. 阶乘后的零
Unity free element special effect recommendation
MySQL:当你CRUD时BufferPool中发生了什么?十张图就能说清楚
Can MySQL export tables regularly?
解决CSDN因版权不明而无法发布博客的问题
Unity免费元素特效推荐
聊天机器人有何用处?有何类型?看完这些就明白了!
数组的子集能否累加出K
buck电路boot和ph引脚实测
How to write controller layer code gracefully?
数据库持久化+JDBC数据库连接
Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
vim文本编辑器的一些使用小技巧