mirror of
https://github.com/cubixle/tuukoti-framework.git
synced 2026-04-24 19:44:48 +01:00
add template reloading
This commit is contained in:
@@ -34,15 +34,25 @@ func (e *ErrFailedToWrite) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HTMLRender struct {
|
type HTMLRender struct {
|
||||||
|
fs fs.FS
|
||||||
|
pattern string
|
||||||
|
reload bool
|
||||||
templates *template.Template
|
templates *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(fs fs.FS, pattern string) (*HTMLRender, error) {
|
func New(fs fs.FS, pattern string, reload bool) (*HTMLRender, error) {
|
||||||
return &HTMLRender{
|
return &HTMLRender{
|
||||||
templates: template.Must(template.ParseFS(fs, pattern)),
|
templates: template.Must(template.ParseFS(fs, pattern)),
|
||||||
|
fs: fs,
|
||||||
|
pattern: pattern,
|
||||||
|
reload: reload,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTMLRender) Render(w io.Writer, templateName string, data interface{}, ctx echo.Context) error {
|
func (h *HTMLRender) Render(w io.Writer, templateName string, data interface{}, ctx echo.Context) error {
|
||||||
|
if h.reload {
|
||||||
|
h.templates = template.Must(template.ParseFS(h.fs, h.pattern))
|
||||||
|
}
|
||||||
|
|
||||||
return h.templates.ExecuteTemplate(w, templateName, data)
|
return h.templates.ExecuteTemplate(w, templateName, data)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user