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("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": "Main website",
		})
	})

	r.Run(":8080")
}

templates/index.tmpl

<html>
	<h1>
		{{ .title }}
	h1>
html>

测试

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

加载多个子目录下的文件

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" }}
<html><h1>
	{{ .title }}
h1>
<p>Using posts/index.tmplp>
html>
{{ end }}

templates/users/index.tmpl

{{ define "users/index.tmpl" }}
<html><h1>
	{{ .title }}
h1>
<p>Using users/index.tmplp>
html>
{{ 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" }}
<html><h1>
	{{ .title }}
h1>
<p>Using posts/index.tmplp>
html>
{{ end }}

vim users.tmpl

{{ define "users.tmpl" }}
<html><h1>
	{{ .title }}
h1>
<p>Using users/index.tmplp>
html>
{{ 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系列-模板引擎

你可能感兴趣的:(Golang,golang,web,gin,web,框架,gin,html模板渲染,自定义模板)