morning-coffee/templates.go

27 lines
531 B
Go

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)
}