package templates import ( "embed" "html/template" "net/http" "go.uber.org/zap" ) //go:embed views/*.html var tmplFS embed.FS var ( Index = parse("views/index.html") Dashboard = parse("views/dashboard.html") ) func parse(file string) *template.Template { return template.Must(template.New("base.html").ParseFS(tmplFS, "views/base.html", file)) } // Renderer renders HTML templates, and logs any errors. type Renderer struct { logger *zap.SugaredLogger } func NewRenderer(logger *zap.SugaredLogger) *Renderer { return &Renderer{logger: logger} } // Render renders an HTML template to an http.ResponseWriter. func (r *Renderer) Render(w http.ResponseWriter, template *template.Template, data any) { if err := template.Execute(w, data); err != nil { r.logger.With("err", err).Error("error rendering template") http.Error(w, "error rendering template", http.StatusInternalServerError) } }