diff options
Diffstat (limited to 'backend/admin/renderer.go')
| -rw-r--r-- | backend/admin/renderer.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/backend/admin/renderer.go b/backend/admin/renderer.go new file mode 100644 index 0000000..468677f --- /dev/null +++ b/backend/admin/renderer.go @@ -0,0 +1,49 @@ +package admin + +import ( + "embed" + "html/template" + "io" + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +var ( + //go:embed templates + templatesFS embed.FS + //go:embed assets + assetsFS embed.FS +) + +type Renderer struct { + templates map[string]*template.Template +} + +func NewRenderer() *Renderer { + return &Renderer{ + templates: make(map[string]*template.Template), + } +} + +func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + tmpl, ok := r.templates[name] + if !ok { + t, err := template.ParseFS(templatesFS, "templates/base.html", "templates/"+name+".html") + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) + } + r.templates[name] = t + tmpl = t + } + return tmpl.ExecuteTemplate(w, name+".html", data) +} + +func newAssetsMiddleware() echo.MiddlewareFunc { + return middleware.StaticWithConfig(middleware.StaticConfig{ + Root: "/assets", + Filesystem: http.FS(assetsFS), + IgnoreBase: true, + }) +} |
