This is a custom HTML render to support multi templates, ie. more than one *template.Template
.
Download and install it:
$ go get github.com/gin-contrib/multitemplate
Import it in your code:
import "github.com/gin-contrib/multitemplate"
package main
import (
"github.com/gin-contrib/multitemplate"
"gopkg.in/gin-gonic/gin.v1"
)
func createMyRender() multitemplate.Render {
r := multitemplate.New()
r.AddFromFiles("index", "templates/base.html", "templates/index.html")
r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html")
return r
}
func main() {
router := gin.Default()
router.HTMLRender = createMyRender()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"title": "Html5 Template Engine",
})
})
router.GET("/article", func(c *gin.Context) {
c.HTML(200, "article", gin.H{
"title": "Html5 Article Engine",
})
})
router.Run(":8080")
}
Approximating html/template Inheritance
package main
import (
"html/template"
"path/filepath"
"github.com/gin-contrib/multitemplate"
"gopkg.in/gin-gonic/gin.v1"
)
func main() {
router := gin.Default()
router.HTMLRender = loadTemplates("./templates")
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index.tmpl", gin.H{
"title": "Welcome!",
})
})
router.Run(":8080")
}
func loadTemplates(templatesDir string) multitemplate.Render {
r := multitemplate.New()
layouts, err := filepath.Glob(templatesDir + "layouts/*.tmpl")
if err != nil {
panic(err.Error())
}
includes, err := filepath.Glob(templatesDir + "includes/*.tmpl")
if err != nil {
panic(err.Error())
}
// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append(includes, layout)
r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...)))
}
return r
}