栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

gin框架03--html 模板渲染

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gin框架03--html 模板渲染

gin框架03--html 模板渲染
  • 介绍
  • 案例
    • 加载 templates 目录下的文件
    • 加载多个子目录下的文件
    • 自定义模板渲染器
    • 自定义模板功能
  • 说明

介绍

gin支持加载HTML模板, 然后根据模板参数进行配置并返回相应的数据,本质上就是字符串替换,既可以使用 LoadHTMLGlob() 或者 LoadHTMLFiles() 方法加载模板文件, 也可以使用自定义模板渲染器。

案例 加载 templates 目录下的文件
package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.LoadHTMLGlob("templates*")
	r.GET("/posts/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
			"title": "Posts",
		})
	})
	r.GET("/users/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "users/index.tmpl", gin.H{
			"title": "Users",
		})
	})

	r.Run(":8080")
}

templates/posts/index.tmpl

{{ define "posts/index.tmpl" }}

{{ .title }}

Using posts/index.tmpl

{{ end }}

templates/users/index.tmpl

{{ define "users/index.tmpl" }}

{{ .title }}

Using users/index.tmpl

{{ end }}

测试

目录结构:
.
├── go.mod
├── go.sum
├── main.go
└── templates
    ├── posts
    │   └── index.tmpl
    └── users
        └── index.tmpl
输出:
http://127.0.0.1:8080/posts/index
Posts
Using posts/index.tmpl

http://127.0.0.1:8080/users/index
Users
Using users/index.tmpl
自定义模板渲染器

我们可以使用自定义的 html 模板渲染, 如下使用 template.ParseFiles 函数,那么第一个模板为 posts.tmpl, 第二个模板为 users.tmpl
vim main.go

package main

import (
	"github.com/gin-gonic/gin"
	"html/template"
	"net/http"
)

func main() {
	router := gin.Default()
	html := template.Must(template.ParseFiles("templates/posts.tmpl",
		"templates/users.tmpl"))
	router.SetHTMLTemplate(html)
	router.GET("/posts", func(c *gin.Context) {
		c.HTML(http.StatusOK, "posts.tmpl", gin.H{
			"title": "posts.tmpl",
		})
	})

	router.GET("/users", func(c *gin.Context) {
		c.HTML(http.StatusOK, "users.tmpl", gin.H{
			"title": "users.tmpl",
		})
	})
	router.Run(":8080")
}

vim posts.tmpl

{{ define "posts.tmpl" }}

{{ .title }}

Using posts/index.tmpl

{{ end }}

vim users.tmpl

{{ define "users.tmpl" }}

{{ .title }}

Using users/index.tmpl

{{ end }}

测试

目录结构:
.
├── go.mod
├── go.sum
├── main.go
└── templates
    ├── posts.tmpl
    └── users.tmpl
输出:
http://127.0.0.1:8080/users
users.tmpl
Using users/index.tmpl

http://127.0.0.1:8080/posts
posts.tmpl
Using posts/index.tmpl
自定义模板功能

默认分隔符为 {{ }}, 我们也可以使用自定义分隔 {[{ }]}, 只需要通过 r.Delims 即可实现

	r := gin.Default()
	r.Delims("{[{", "}]}")
	r.LoadHTMLGlob("/path/to/templates")

main.go

package main

import (
	"fmt"
	"html/template"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

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.LoadHTMLFiles("./testdata/raw.tmpl")

	router.GET("/raw", func(c *gin.Context) {
		c.HTML(http.StatusOK, "raw.tmpl", gin.H{
			"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
		})
	})

	router.Run(":8080")
}

testdata/raw.tmpl

Date: {[{.now | formatAsDate}]}

测试

目录结构:
.
├── go.mod
├── go.sum
├── main.go
└── testdata
    └── raw.tmpl
输出:
http://127.0.0.1:8080/raw
Date: 201707/01
说明

软件环境:
ubuntu 20.04.1 LTS Desktop
go version go1.17.6 linux/amd64
参考文档:
gin官方文档–HTML 渲染
gin中文文档–html 渲染
golang自定义模板示例
gin系列-模板引擎

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号