Add FuncMap to From* functions
This commit is contained in:
parent
ff031a68a0
commit
3cfd99c254
@ -49,7 +49,7 @@ func (t Tmpl) WriteContentType(res http.ResponseWriter) {
|
|||||||
res.Header().Set("Content-Type", "text/html; charset=utf-8")
|
res.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromFS(f fs.FS, root, ext string) (Map, error) {
|
func FromFS(f fs.FS, root, ext string, fm template.FuncMap) (Map, error) {
|
||||||
tmpls := Map{}
|
tmpls := Map{}
|
||||||
err := fs.WalkDir(f, root, func(path string, d fs.DirEntry, err error) error {
|
err := fs.WalkDir(f, root, func(path string, d fs.DirEntry, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,7 +62,13 @@ func FromFS(f fs.FS, root, ext string) (Map, error) {
|
|||||||
tmplName := strings.TrimPrefix(path, root+"/")
|
tmplName := strings.TrimPrefix(path, root+"/")
|
||||||
tmplName = strings.TrimSuffix(tmplName, ext)
|
tmplName = strings.TrimSuffix(tmplName, ext)
|
||||||
|
|
||||||
tmpl, err := template.New(filepath.Base(path)).ParseFS(f, path, filepath.Join(root, "base"+ext))
|
tmpl := template.New(filepath.Base(path))
|
||||||
|
|
||||||
|
if fm != nil {
|
||||||
|
tmpl = tmpl.Funcs(fm)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err = tmpl.ParseFS(f, path, filepath.Join(root, "base"+ext))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -74,8 +80,8 @@ func FromFS(f fs.FS, root, ext string) (Map, error) {
|
|||||||
return tmpls, err
|
return tmpls, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromDir(baseDir, ext string) (Map, error) {
|
func FromDir(baseDir, ext string, fm template.FuncMap) (Map, error) {
|
||||||
dir := filepath.Dir(baseDir)
|
dir := filepath.Dir(baseDir)
|
||||||
root := filepath.Base(baseDir)
|
root := filepath.Base(baseDir)
|
||||||
return FromFS(os.DirFS(dir), root, ext)
|
return FromFS(os.DirFS(dir), root, ext, fm)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user