d821d392/html_components/render-go-html.go
1970-01-01 00:00:00 +00:00

41 lines
1.1 KiB
Go

package html_components
import (
. "github.com/labstack/echo/v4"
"html/template"
)
type GoHtmlHandler struct {
tmpl *template.Template
}
func NewGoHtmlHandler() *GoHtmlHandler {
tmpl := template.Must(template.ParseGlob("views/components/*.gohtml"))
tmpl = template.Must(tmpl.ParseGlob("views/crud/*.gohtml"))
tmpl = template.Must(tmpl.ParseGlob("views/base*.gohtml"))
return &GoHtmlHandler{tmpl: tmpl}
}
func (h *GoHtmlHandler) RenderPage(c Context, name string, data any) error {
tmpl := template.Must(h.tmpl.Clone())
tmpl = template.Must(tmpl.ParseGlob(GetTemplatePath(name)))
return tmpl.ExecuteTemplate(c.Response().Writer, "base", data)
}
func (h *GoHtmlHandler) RenderComponent(c Context, name string, data any) error {
tmpl := template.Must(h.tmpl.Clone())
return tmpl.ExecuteTemplate(c.Response().Writer, name, data)
}
func RenderGoHtmlPage(c Context, name string, data any) error {
return NewGoHtmlHandler().RenderPage(c, name, data)
}
func RenderGoHtmlComponent(c Context, name string, data any) error {
return NewGoHtmlHandler().RenderComponent(c, name, data)
}
func GetTemplatePath(name string) string {
return "views/" + name + "*.gohtml"
}