node-api/templates.go

30 lines
554 B
Go
Raw Normal View History

2020-07-13 22:01:42 +00:00
package main
import (
"html/template"
"io"
"github.com/gobuffalo/packr"
2024-01-26 17:46:19 +00:00
"github.com/labstack/echo/v4"
2020-07-13 22:01:42 +00:00
)
// Template struct
type Template struct{}
// Render template saved in packr
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
box := packr.NewBox("./ui")
tmpl, err := template.New(name).Parse(box.String("index.html"))
if err != nil {
panic(err)
}
tmpl, err = tmpl.Parse(box.String(name))
if err != nil {
panic(err)
}
response := tmpl.ExecuteTemplate(w, name, data)
return response
}