تخطَّ إلى المحتوى

عرض HTML

يستخدم Gin حزمة html/template لعرض HTML. لمزيد من المعلومات حول كيفية استخدامها، بما في ذلك العناصر النائبة المتاحة، راجع وثائق text/template

استخدم LoadHTMLGlob() أو LoadHTMLFiles() لتحديد ملفات HTML المراد تحميلها.

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

ملاحظة: يرجى تغليف قالب HTML الخاص بك في كتلة {{define <template-path>}} {{end}} وتعريف ملف القالب بالمسار النسبي <template-path>. وإلا فلن يتمكن GIN من تحليل ملفات القالب بشكل صحيح.

templates/posts/index.tmpl

{{ define "posts/index.tmpl" }}
<html><h1>
{{ .title }}
</h1>
<p>Using posts/index.tmpl</p>
</html>
{{ end }}

templates/users/index.tmpl

{{ define "users/index.tmpl" }}
<html><h1>
{{ .title }}
</h1>
<p>Using users/index.tmpl</p>
</html>
{{ end }}

تحميل القوالب من http.FileSystem (الإصدار 1.11+)

إذا كانت قوالبك مضمنة أو مقدمة من http.FileSystem، استخدم LoadHTMLFS:

import (
"embed"
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed templates
var 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"
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

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

النتيجة:

Terminal window
Date: 2017/07/01