mirror of https://git.sr.ht/~statianzo/todogo
parent
3dce44a652
commit
7e01f3937d
@ -0,0 +1,36 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//go:embed *.html
|
||||
var tmplFS embed.FS
|
||||
|
||||
func New() *Views {
|
||||
templates := template.Must(template.New("").ParseFS(tmplFS, "*.html"))
|
||||
|
||||
return &Views{
|
||||
templates: templates,
|
||||
}
|
||||
}
|
||||
|
||||
type Views struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
func (v *Views) Renderer(name string) func(w http.ResponseWriter, r *http.Request, data any) {
|
||||
tmpl := template.Must(v.templates.Clone())
|
||||
tmpl = template.Must(tmpl.ParseFS(tmplFS, name))
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request, data any) {
|
||||
err := tmpl.ExecuteTemplate(w, "layout.html", data)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue