26 lines
545 B
Go
26 lines
545 B
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type Templates struct {
|
|
tmpl *template.Template
|
|
}
|
|
|
|
func NewTemplates(tmpl *template.Template) *Templates {
|
|
return &Templates{tmpl: tmpl}
|
|
}
|
|
func (t *Templates) Render(w http.ResponseWriter, name string, data any) {
|
|
var buf bytes.Buffer
|
|
if err := t.tmpl.ExecuteTemplate(&buf, name+".html", data); err != nil {
|
|
log.Printf("template error: %s: %v", name, err)
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Write(buf.Bytes())
|
|
}
|