2022-04-16 21:34:19 +03:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2022-04-16 23:12:23 +03:00
|
|
|
"fmt"
|
2022-04-16 21:34:19 +03:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed *.html
|
|
|
|
var templateFS embed.FS
|
|
|
|
|
|
|
|
type Template struct {
|
|
|
|
funcMap template.FuncMap
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(funcMap template.FuncMap) *Template {
|
|
|
|
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(templateFS, "*.html"))
|
|
|
|
return &Template{
|
|
|
|
funcMap: funcMap,
|
|
|
|
templates: templates,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDebug(funcMap template.FuncMap) *Template {
|
2022-04-16 23:12:23 +03:00
|
|
|
ts := template.Must(template.New("").Funcs(funcMap).ParseGlob("templates/*.html"))
|
2022-04-16 21:34:19 +03:00
|
|
|
return &Template{
|
|
|
|
funcMap: funcMap,
|
2022-04-16 23:12:23 +03:00
|
|
|
templates: ts,
|
2022-04-16 21:34:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var DebugTemplates = true
|
|
|
|
|
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
|
|
|
if DebugTemplates {
|
2022-04-16 23:12:23 +03:00
|
|
|
fmt.Println("RENDER", name)
|
|
|
|
tmpl := NewDebug(t.funcMap).templates
|
|
|
|
tmpl = template.Must(tmpl.ParseFiles("templates/" + name))
|
|
|
|
return tmpl.ExecuteTemplate(w, name, data)
|
2022-04-16 21:34:19 +03:00
|
|
|
}
|
2022-04-16 23:12:23 +03:00
|
|
|
tmpl := template.Must(t.templates.Clone())
|
|
|
|
tmpl = template.Must(tmpl.ParseFS(templateFS, name))
|
|
|
|
return tmpl.ExecuteTemplate(w, name, data)
|
2022-04-16 21:34:19 +03:00
|
|
|
}
|