mirror of https://git.sr.ht/~statianzo/todogo
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
679 B
37 lines
679 B
2 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|