From ee981f128a69f4d97aa8c4500941e4b784750a33 Mon Sep 17 00:00:00 2001 From: flybread <1015126672@qq.com> Date: Tue, 17 Jun 2025 16:09:29 +0800 Subject: [PATCH] gin example --- examples/url/bind_template/main.go | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/url/bind_template/main.go diff --git a/examples/url/bind_template/main.go b/examples/url/bind_template/main.go new file mode 100644 index 00000000..6ed7f372 --- /dev/null +++ b/examples/url/bind_template/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "embed" + "html/template" + "net/http" + + "github.com/gin-gonic/gin" +) + +//go:embed assets/* templates/* +var f embed.FS + +func main() { + router := gin.Default() + templ := template.Must(template.New("").ParseFS(f, "templates/*.tmpl", "templates/foo/*.tmpl")) + router.SetHTMLTemplate(templ) + + // example: /public/assets/images/example.png + router.StaticFS("/public", http.FS(f)) + + router.GET("/", func(c *gin.Context) { + c.HTML(http.StatusOK, "index.tmpl", gin.H{ + "title": "Main website", + }) + }) + + router.GET("/foo", func(c *gin.Context) { + c.HTML(http.StatusOK, "bar.tmpl", gin.H{ + "title": "Foo website", + }) + }) + + router.GET("favicon.ico", func(c *gin.Context) { + file, _ := f.ReadFile("assets/favicon.ico") + c.Data( + http.StatusOK, + "image/x-icon", + file, + ) + }) + + router.Run(":8080") +}