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