当前位置:网站首页>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 course 10 linear programming and simplex method (discussion on detection number and degradation)
- [cf1054h] epic Revolution -- number theory, convolution, arbitrary modulus NTT
- vscode通过remotessh结合xdebug远程调试php解决方案
- Teacher Cui Xueting's course notes on optimization theory and methods 00 are written in the front
- Teacher Wu Enda's machine learning course notes 02 univariate linear regression
- Excerpts from good essays
- JVM之垃圾回收机制(GC)
- Leetcode-592: fraction addition and subtraction
- Decompilation of wechat applet
- 王树尧老师运筹学课程笔记 00 写在前面
猜你喜欢

MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear

实现改变一段文字的部分颜色效果

IDEA中实现Mapper接口到映射文件xml的跳转

约瑟夫环问题

Revolution of game assets

Apisik health check test

JVM之垃圾回收机制(GC)

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

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

Sword finger offer II 115: reconstruction sequence
随机推荐
vim文本编辑器的一些使用小技巧
DM数据守护集群搭建
Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)
实战!聊聊如何解决MySQL深分页问题
10 frequently asked JVM questions in interviews
vscode通过remotessh结合xdebug远程调试php解决方案
MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of
Unity free element special effect recommendation
Cvpr2022oral special series (I): low light enhancement
How to write controller layer code gracefully?
Unity探索地块通路设计分析 & 流程+代码具体实现
CVPR2022Oral专题系列(一):低光增强
好文佳句摘录
Federal learning backdoor attack summary (2019-2022)
聊天机器人有何用处?有何类型?看完这些就明白了!
Analog volume leetcode [normal] 093. Restore IP address
Leetcode-1331: array ordinal conversion
Unity免费元素特效推荐
数组的子集不能累加出的最小正数
Hj37 statistics of the total number of rabbits per month Fibonacci series