Add `Engine.LoadHTMLFromFS()` method to support `go:embed`

This commit is contained in:
Danny Hermes 2022-02-08 18:51:30 -06:00 committed by GitHub
parent 87e40d6b15
commit ad70fbf73a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

18
gin.go
View File

@ -7,6 +7,7 @@ package gin
import (
"fmt"
"html/template"
"io/fs"
"net"
"net/http"
"os"
@ -267,6 +268,23 @@ func (engine *Engine) LoadHTMLFiles(files ...string) {
engine.SetHTMLTemplate(templ)
}
// LoadHTMLFromFS loads HTML files identified by glob pattern
// from a filesytem interface and associates the result with HTML renderer.
func (engine *Engine) LoadHTMLFromFS(filesystem fs.FS, patterns ...string) {
left := engine.delims.Left
right := engine.delims.Right
templ := template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseFS(filesystem, patterns...))
if IsDebugging() {
debugPrintLoadTemplate(templ)
engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
return
}
engine.SetHTMLTemplate(templ)
}
// SetHTMLTemplate associate a template with HTML renderer.
func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
if len(engine.trees) > 0 {