Refactor FromFile constructor

This commit is contained in:
Lucas Schumacher 2024-08-12 18:04:22 -04:00
parent de681c1ac3
commit da5eeb3f0f

View File

@ -7,6 +7,7 @@ import (
"io/fs" "io/fs"
"log" "log"
"net/http" "net/http"
"os"
) )
type Template struct { type Template struct {
@ -26,14 +27,9 @@ func (t Template) ExecuteWriter(w io.Writer, data interface{}) error {
return t.htmlTpl.Execute(w, data) return t.htmlTpl.Execute(w, data)
} }
func FromFile(filepath string) (Template, error) { func FromFile(pattern ...string) (Template, error) {
tpl, err := template.ParseFiles(filepath) fs := os.DirFS(".")
if err != nil { return FromFS(fs, pattern...)
return Template{}, fmt.Errorf("Error parsing template: %v", err)
}
return Template{
htmlTpl: tpl,
}, nil
} }
func FromFS(fs fs.FS, pattern ...string) (Template, error) { func FromFS(fs fs.FS, pattern ...string) (Template, error) {
tpl, err := template.ParseFS(fs, pattern...) tpl, err := template.ParseFS(fs, pattern...)