templates.go (747B)
1 package templates 2 3 import ( 4 "embed" 5 "fmt" 6 "html/template" 7 "io/fs" 8 "strings" 9 ) 10 11 //go:embed static/* 12 var static embed.FS 13 14 const fileType = ".gohtml" 15 16 var funcs = template.FuncMap{} 17 18 func Parse() (*template.Template, error) { 19 var tmpl = template.New("").Funcs(funcs) 20 21 parseFS := func(path string, d fs.DirEntry, err error) error { 22 if err != nil { 23 return err 24 } 25 if d.IsDir() { 26 return nil 27 } 28 29 if strings.Contains(path, fileType) { 30 if _, err := tmpl.ParseFS(static, path); err != nil { 31 return fmt.Errorf( 32 "failed to parse template: %v", err, 33 ) 34 } 35 } 36 37 return nil 38 } 39 40 if err := fs.WalkDir(static, ".", parseFS); err != nil { 41 return nil, fmt.Errorf("failed to parse templates: %v", err) 42 } 43 44 return tmpl, nil 45 }