morning-coffee/render_templates.go

58 lines
1.3 KiB
Go

// Template rendering
//
// Abstracts the rendering of static HTML templates.
// Render(...) handles rendering the provided template with the
// default partial templates (header, footer, general base...), handles
// getting the paths to those template files and feeding the data (including
// some internally-generated context) when hydrating the template.
package main
import (
"fmt"
"io"
"path/filepath"
"text/template"
"time"
)
type RenderMeta struct {
RenderStart time.Time
Location string
}
type RenderContext struct {
Data interface{}
Meta RenderMeta
}
func getRenderDuration(start time.Time) time.Duration {
return time.Now().Sub(start)
}
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"),
asTemplatePath("footer"),
}
start := time.Now()
tmpl := template.New(templateName).Funcs(template.FuncMap{
"getRenderDuration": getRenderDuration,
})
tmpl, err := tmpl.ParseFiles(templatePartials...)
if err != nil {
return err
}
renderContext := RenderContext{Data: data, Meta: RenderMeta{RenderStart: start, Location: "America/New_York"}}
return tmpl.ExecuteTemplate(w, "base", renderContext)
}