templates/init.go

29 lines
855 B
Go
Raw Permalink Normal View History

2025-01-12 10:47:45 +00:00
package templates
import (
"embed"
"html/template"
)
type Templates map[string]*template.Template
// NewTemplates creates a new responder (embedded_dir should have a trailing slash)
func NewTemplates(given_fs embed.FS, embedded_dir string, variadics ...string) Templates {
// todo later: allow both io/fs.FS and embed.FS as called arguments
templates := make(map[string]*template.Template)
pages, _ := given_fs.ReadDir(embedded_dir)
for _, page := range pages {
file_path := embedded_dir + page.Name()
files := []string{file_path}
for _, v := range variadics {
files = append(files, embedded_dir+v) // add prefix
}
// files = append(files, variadics...)
tpl := template.Must(
template.New(page.Name()).ParseFS(given_fs, files...)) //+".html"))
templates[page.Name()] = tpl
// fmt.Println(page.Name())
}
return templates
}