govote/http/template_renderer.go
2024-05-14 10:27:13 +02:00

38 lines
877 B
Go

// labstack/echo requires a custom TemplateRenderer.
// This module implements it.
package http
import (
"io"
"text/template"
"github.com/labstack/echo"
)
// Template is a data-container for a template.
type Template struct {
Templates *template.Template
}
// Render injects the echo.Context into the template renderer.
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.Templates.ExecuteTemplate(w, name, data)
}
// NewTemplateRenderer returns an initialized Template and injects the patched Renderer.
func NewTemplateRenderer(e *echo.Echo, paths ...string) {
tmpl := &template.Template{}
for i := range paths {
template.Must(tmpl.ParseGlob(paths[i]))
}
t := newTemplate(tmpl)
e.Renderer = t
}
func newTemplate(templates *template.Template) echo.Renderer {
return &Template{
Templates: templates,
}
}