How to use the LoadHTMLGlob method to reference template files from two different directory levels simultaneously?
The template directory structure is as follows:
tpl/
abc/index.html
index.html
main.go
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
//gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.LoadHTMLGlob("tpl/**/*")
//r.LoadHTMLFiles("tpl/index.html", "tpl/abc/index.html")
r.GET("/index", func(c *gin.Context) {
data := &struct {
Title string
Content string
}{
Title: "News title",
Content: "This is the content of the news page.",
}
c.HTML(200, "index.html", gin.H{
"title": "News Page",
"news": data,
})
})
r.GET("/abc", func(c *gin.Context) {
data := &struct {
Title string
Content string
}{
Title: "News title",
Content: "This is the content of the news page.",
}
c.HTML(200, "abc/index.html", gin.H{
"title": "News Page",
"news": data,
})
})
r.GET("/hello", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello, World!",
})
})
r.Run()
}
tpl/index.html
{{ define "index.html" }}
index
{{ end }}
tpl/abc/index.html
{{ define "abc/index.html" }}
abc / index
{{ end }}