package main

import (
	"html/template"
	"io"

	"github.com/labstack/echo/v4"
)

type Renderer struct {
	template *template.Template
	debug    bool
	location string
}

func NewRenderer(location string, debug bool) *Renderer {
	r := Renderer{}
	r.location = location
	r.debug = debug
	r.ReloadTemplates()
	return &r
}

func (r *Renderer) ReloadTemplates() {
	r.template = template.Must(template.ParseGlob(r.location))
}

func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
	if r.debug {
		r.ReloadTemplates()
	}
	return r.template.ExecuteTemplate(w, name, data)
}
