Default content
{{end}}Gin 框架有内置的模板引擎,它允许你将数据和 HTML 模板结合,动态生成网页内容。
以下是一个简单的使用单个 HTML 模板文件的示例,展示了如何在 Gin 中渲染模板:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
// 加载单个模板文件
r.LoadHTMLFiles("templates/index.html")
r.GET("/", func(c *gin.Context) {
// 渲染模板并传递数据
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Gin Template Example",
"message": "Welcome to Gin's template engine!",
})
})
r.Run(":8080")
}
对应的 templates/index.html
文件内容:
{{.title}}
{{.message}}
在上述代码中,LoadHTMLFiles
方法用于加载指定的 HTML 模板文件,c.HTML
方法用于渲染模板并向模板传递数据。
如果有多个模板文件,可以使用 LoadHTMLGlob
方法加载整个目录下的模板文件:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
// 加载 templates 目录下的所有 HTML 模板文件
r.LoadHTMLGlob("templates/**/*.html")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Gin Multiple Templates Example",
"message": "This is a multi - template example.",
})
})
r.Run(":8080")
}
这里 LoadHTMLGlob
方法使用通配符 **
来递归加载 templates
目录下的所有 HTML 文件。
Gin 的模板引擎基于 Go 的标准模板库,支持以下常见语法:
{{.VariableName}}
输出变量的值,如 {{.title}}
。{{if .condition}}
Condition is true.
{{else}}
Condition is false.
{{end}}
{{range .items}}
{{.}}
{{end}}
Gin 支持模板继承,允许你创建一个基础模板,然后在其他模板中继承和扩展它。
templates/base.html
{{block "title" .}}Default Title{{end}}
My Website
{{block "content" .}}
Default content
{{end}}
templates/index.html
{{define "title"}}Home Page{{end}}
{{define "content"}}
Welcome to the Home Page
This is the home page content.
{{end}}
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.LoadHTMLGlob("templates/**/*.html")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.Run(":8080")
}
在上述代码中,base.html
是基础模板,定义了网站的整体结构和一些默认内容,index.html
继承了 base.html
并覆盖了 title
和 content
块。
你可以向模板引擎中添加自定义函数,以满足特定的需求。
package main
import (
"github.com/gin-gonic/gin"
"html/template"
"net/http"
)
// 自定义函数,将字符串转换为大写
func toUpper(s string) string {
return template.HTMLEscapeString(strings.ToUpper(s))
}
func main() {
r := gin.Default()
// 添加自定义函数
r.SetFuncMap(template.FuncMap{
"toUpper": toUpper,
})
r.LoadHTMLFiles("templates/index.html")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"text": "hello world",
})
})
r.Run(":8080")
}
对应的 templates/index.html
文件可以使用这个自定义函数:
Custom Function Example
{{toUpper .text}}
Gin 框架里,你能够借助 Go 标准模板库的特性,让一个模板包含另一个模板,同时还能向包含的模板传递数据
{{define}}
标签来明确其名称。LoadHTMLGlob
或者 LoadHTMLFiles
方法加载所有相关的模板文件。{{template}}
标签来包含其他模板,并且传递所需的数据。c.HTML
方法渲染主模板。package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
// 加载 templates 目录下的所有 HTML 模板文件
r.LoadHTMLGlob("templates/**/*.html")
r.GET("/", func(c *gin.Context) {
// 定义要传递的数据
data := gin.H{
"pageTitle": "Home Page",
"headerTitle": "Welcome to My Website",
}
// 渲染 base.html 模板并传递数据
c.HTML(http.StatusOK, "base.html", data)
})
r.Run(":8080")
}
templates/base.html
文件
{{.pageTitle}}
{{template "header" .}}
This is the main content of the page.
templates/header.html
文件
{{define "header"}}
{{.headerTitle}}
{{end}}
LoadHTMLGlob
方法加载 templates
目录下的所有 HTML 模板文件。base.html
模板,并且传递一个包含 pageTitle
和 headerTitle
的数据对象。templates/header.html
文件:
{{define "header"}}
标签定义了一个名为 header
的模板。headerTitle
的标题。templates/base.html
文件:
{{template "header" .}}
标签包含了名为 header
的模板,并将当前的数据对象传递给它。从前面的 Gin 框架示例(路由、中间件、模板引擎、文件上传下载等)可以看出,框架通过封装底层逻辑、提供标准化接口、抽象复杂操作等方式,显著简化了 Web 开发流程。