orangesite/hn/templates.go

41 lines
962 B
Go
Raw Normal View History

2020-03-24 20:32:35 +00:00
package hn
import (
"errors"
"html/template"
"io"
"github.com/labstack/echo"
)
// Define the template registry struct
type TemplateRegistry struct {
templates map[string]*template.Template
}
// Implement e.Renderer interface
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tmpl, ok := t.templates[name]
if !ok {
err := errors.New("Template not found -> " + name)
return err
}
return tmpl.ExecuteTemplate(w, "base.html", data)
}
func NewTemplate(templatesDir string) *TemplateRegistry {
//ext := ".html"
ins := TemplateRegistry{
templates: map[string]*template.Template{},
}
//layout := templatesDir + "base" + ext
templates := make(map[string]*template.Template)
templates["index"] = template.Must(template.ParseFiles("_pages/index.html", "_layouts/base.html"))
//templates["about.html"] = template.Must(template.ParseFiles("view/about.html", "view/base.html"))
return &ins
}