dip/templates.go
2020-01-26 16:38:57 +01:00

43 lines
968 B
Go

package main
import (
//"bytes"
//"fmt"
"html/template"
"io"
"strings"
//"github.com/gobuffalo/packr"
"github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4"
)
// Template is a template struct
type Template struct {
templates *template.Template
}
// Render is a method that render templates
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) (err error) {
if strings.HasSuffix(name, ".html") {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
}
return t.templates.ExecuteTemplate(w, name, data)
}
// BuildTemplates converts packr packages to html/template
func BuildTemplates(box *packr.Box) (builttemplates *Template, err error) {
tmpl := template.New("templates")
for _, filename := range box.List() {
tmplContent, _ := box.FindString(filename)
tmpl.New(filename).Parse(tmplContent)
}
builttemplate := &Template{
templates: tmpl,
}
return builttemplate, nil
}