morning-coffee/templates.go

28 lines
531 B
Go
Raw Normal View History

2024-09-21 04:50:10 +00:00
package main
import (
"fmt"
"io"
"path/filepath"
"text/template"
)
func Render(w io.Writer, templateName string, data interface{}) error {
asTemplatePath := func(templateName string) string {
return filepath.Join("templates", fmt.Sprintf("%s.html.tmpl", templateName))
}
templatePartials := []string{
asTemplatePath(templateName),
asTemplatePath("base"),
}
tmpl, err := template.New(templateName).ParseFiles(templatePartials...)
if err != nil {
return err
}
return tmpl.ExecuteTemplate(w, "base", data)
}