Add template embedding functions
This commit is contained in:
parent
88d10f976c
commit
7cfafda526
|
@ -0,0 +1,39 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/gin-gonic/gin"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func LoadHTMLFromEmbedFS(engine *gin.Engine, embedFS embed.FS, pattern string) {
|
||||
root := template.New("")
|
||||
tmpl := template.Must(root, LoadAndAddToRoot(engine.FuncMap, root, embedFS, pattern))
|
||||
engine.SetHTMLTemplate(tmpl)
|
||||
}
|
||||
func LoadAndAddToRoot(funcMap template.FuncMap, rootTemplate *template.Template, embedFS embed.FS, pattern string) error {
|
||||
pattern = strings.ReplaceAll(pattern, ".", "\\.")
|
||||
pattern = strings.ReplaceAll(pattern, "*", ".*")
|
||||
|
||||
err := fs.WalkDir(embedFS, ".", func(path string, d fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
|
||||
if matched, _ := regexp.MatchString(pattern, path); !d.IsDir() && matched {
|
||||
data, readErr := embedFS.ReadFile(path)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
}
|
||||
t := rootTemplate.New(path).Funcs(funcMap)
|
||||
if _, parseErr := t.Parse(string(data)); parseErr != nil {
|
||||
return parseErr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
Loading…
Reference in New Issue