HTML 渲染
使用 LoadHTMLGlob() 或 LoadHTMLFiles()
import ( "net/http"
"github.com/gin-gonic/gin")
func main() { router := gin.Default() router.LoadHTMLGlob("templates/*") //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") router.GET("/index", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", gin.H{ "title": "主網站", }) }) router.Run(":8080")}templates/index.tmpl
<html> <h1> {{ .title }} </h1></html>在不同目錄中使用同名樣板
func main() { router := gin.Default() router.LoadHTMLGlob("templates/**/*") router.GET("/posts/index", func(c *gin.Context) { c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ "title": "文章", }) }) router.GET("/users/index", func(c *gin.Context) { c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ "title": "使用者", }) }) router.Run(":8080")}templates/posts/index.tmpl
{{ define "posts/index.tmpl" }}<html><h1> {{ .title }}</h1><p>正在使用 posts/index.tmpl</p></html>{{ end }}templates/users/index.tmpl
{{ define "users/index.tmpl" }}<html><h1> {{ .title }}</h1><p>正在使用 users/index.tmpl</p></html>{{ end }}注意: 請將您的 HTML 樣板包裝在 {{define <template-path>}} {{end}} 區塊中,並使用相對路徑 <template-path> 定義您的樣板檔案。否則,GIN 將無法正確解析樣板檔案。
從 http.FileSystem 載入樣板 (v1.11+)
如果您的樣板是嵌入的或由 http.FileSystem 提供,請使用 LoadHTMLFS:
import ( "embed" "io/fs" "net/http" "github.com/gin-gonic/gin")
//go:embed templatesvar tmplFS embed.FS
func main() { r := gin.Default() sub, _ := fs.Sub(tmplFS, "templates") r.LoadHTMLFS(http.FS(sub), "**/*.tmpl") r.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", gin.H{"title": "From FS"}) }) r.Run(":8080")}自訂樣板渲染器
您也可以使用自己的 HTML 樣板渲染器
import "html/template"
import ( "html/template"
"github.com/gin-gonic/gin")
func main() { router := gin.Default() html := template.Must(template.ParseFiles("file1", "file2")) router.SetHTMLTemplate(html) router.Run(":8080")}自訂分隔符號
您可以使用自訂分隔符號
router := gin.Default() router.Delims("{[{", "}]}") router.LoadHTMLGlob("/path/to/templates")自訂樣板函式
請參閱詳細的範例程式碼。
main.go
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/template/raw.tmpl")
router.GET("/raw", func(c *gin.Context) { c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{ "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC), }) })
router.Run(":8080")}raw.tmpl
日期: {[{.now | formatAsDate}]}結果:
日期: 2017/07/01