46 lines
988 B
Go
46 lines
988 B
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"io/fs"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/web"
|
|
"github.com/gin-gonic/gin/render"
|
|
)
|
|
|
|
type Templates struct {
|
|
templates map[string]*template.Template
|
|
}
|
|
|
|
func NewTemplates(funcMap template.FuncMap) (*Templates, error) {
|
|
templates, err := fs.Glob(web.Templates, "*.tpl")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := &Templates{
|
|
templates: make(map[string]*template.Template, 0),
|
|
}
|
|
pages, err := fs.Glob(web.Templates, "*.html")
|
|
for _, page := range pages {
|
|
allTemplates := append([]string{page}, templates...)
|
|
tpl, err := template.New(page).Funcs(funcMap).ParseFS(web.Templates, allTemplates...)
|
|
fmt.Printf("page: %s, templates: %v\n", page, templates)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result.templates[page] = tpl
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (tpl *Templates) Instance(name string, obj interface{}) render.Render {
|
|
return render.HTML{
|
|
Template: tpl.templates[name],
|
|
Name: name,
|
|
Data: obj,
|
|
}
|
|
}
|