gin中HTML渲染
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 | package main import ( "github.com/gin-gonic/gin" "net/http" ) func login(ctx *gin.Context) { ctx.JSON(http.StatusOK, map [string] interface {}{ "username" : "李四" , "password" : 123465, }) } func main() { // HTML渲染, router := gin.Default() //router.LoadHTMLFiles("templates/index.html", "templates/login.html") //router.LoadHTMLGlob("templates/*") // 使用不同目录下名称相同的模板 router.LoadHTMLGlob( "templates/**/*" ) router.GET( "/users/index" , func (context *gin.Context) { context.HTML(http.StatusOK, "users/index.html" , gin.H{ "title" : "Users" , }) }) router.GET( "/center/index" , func (context *gin.Context) { context.HTML(http.StatusOK, "center/index.html" , gin.H{ "title" : "Center" , }) }) router.GET( "/login" , login) router.Run() } |
目录结构
html代码
1 2 3 4 5 6 7 8 9 10 11 12 | <! DOCTYPE html> {{ define "center/index.html" }} < html lang="en"> < head > < meta charset="UTF-8"> < title >首页</ title > </ head > < body > < h1 >{{ .title }}</ h1 > </ body > </ html > {{ end }} |
2. 自定义HTML模板渲染器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package main import ( "github.com/gin-gonic/gin" "html/template" "net/http" ) func main() { router := gin.Default() // 自定义html模板渲染器,要指定所有的html路径,不推荐 html := template.Must(template.ParseFiles( "templates/login.html", "templates/users/index.html", "templates/center/index.html", )) router.SetHTMLTemplate(html) router.GET("/users/index", func(context *gin.Context) { context.HTML(http.StatusOK, "users/index.html", gin.H{ "title": "users/index.html", }) }) router.Run() } |
3. 自定义分隔符、模板功
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 | package main import ( "fmt" "github.com/gin-gonic/gin" "html/template" "net/http" "time" ) func formatAsDate(t time.Time) string { year, month, day := t.Date() return fmt.Sprintf( "%d-%02d-%02d" , year, month, day) } func main() { router := gin.Default() // 自定义分隔符 router.Delims( "{[{" , "}]}" ) // 自定义模板功能 router.SetFuncMap(template.FuncMap{ "formatAsDate" : formatAsDate, }) // 加载模板文件路径 router.LoadHTMLGlob( "templates/**/*" ) router.GET( "/users/index" , func (context *gin.Context) { context.HTML(http.StatusOK, "users/index.html" , map [string] interface {}{ "now" : time.Date(2021, 10, 15, 0, 0, 0, 0, time.Local), }) }) router.Run() } |
hmtl代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <! DOCTYPE html> {[{ define "users/index.html" }]} < html lang="en"> < head > < meta charset="UTF-8"> < title >Title</ title > </ head > < body > < h2 > {[{ .now | formatAsDate }]} </ h2 > </ body > </ html > {[{ end }]} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)