|
|
@ -8,8 +8,15 @@ import (
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var isDevMode bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
isDevMode = os.Getenv("MODE") == "development"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TemplateRenderer holds cached templates for rendering
|
|
|
|
// TemplateRenderer holds cached templates for rendering
|
|
|
|
type TemplateRenderer struct {
|
|
|
|
type TemplateRenderer struct {
|
|
|
|
|
|
|
|
baseFile string
|
|
|
|
baseTemplate *template.Template
|
|
|
|
baseTemplate *template.Template
|
|
|
|
templateMap map[string]*template.Template
|
|
|
|
templateMap map[string]*template.Template
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -17,6 +24,7 @@ type TemplateRenderer struct {
|
|
|
|
// NewTemplateRenderer constructs a template renderer with a base file
|
|
|
|
// NewTemplateRenderer constructs a template renderer with a base file
|
|
|
|
func NewTemplateRenderer(baseFile string) *TemplateRenderer {
|
|
|
|
func NewTemplateRenderer(baseFile string) *TemplateRenderer {
|
|
|
|
return &TemplateRenderer{
|
|
|
|
return &TemplateRenderer{
|
|
|
|
|
|
|
|
baseFile: baseFile,
|
|
|
|
baseTemplate: template.Must(template.ParseFiles("./views/" + baseFile)),
|
|
|
|
baseTemplate: template.Must(template.ParseFiles("./views/" + baseFile)),
|
|
|
|
templateMap: make(map[string]*template.Template),
|
|
|
|
templateMap: make(map[string]*template.Template),
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -24,9 +32,16 @@ func NewTemplateRenderer(baseFile string) *TemplateRenderer {
|
|
|
|
|
|
|
|
|
|
|
|
// Render the template
|
|
|
|
// Render the template
|
|
|
|
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
|
|
|
|
|
|
|
|
tmpl := t.templateMap[name]
|
|
|
|
tmpl := t.templateMap[name]
|
|
|
|
if os.Getenv("MODE") == "development" || tmpl == nil {
|
|
|
|
if isDevMode || tmpl == nil {
|
|
|
|
tmpl = template.Must(t.baseTemplate.Clone())
|
|
|
|
|
|
|
|
|
|
|
|
if isDevMode {
|
|
|
|
|
|
|
|
tmpl = template.Must(template.ParseFiles("./views/" + t.baseFile))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
tmpl = template.Must(t.baseTemplate.Clone())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tmpl.ParseFiles("./views/" + name)
|
|
|
|
tmpl.ParseFiles("./views/" + name)
|
|
|
|
t.templateMap[name] = tmpl
|
|
|
|
t.templateMap[name] = tmpl
|
|
|
|
}
|
|
|
|
}
|
|
|
|