2022-09-14 13:30:59 +00:00
|
|
|
package templates
|
|
|
|
|
2022-09-18 14:06:27 +00:00
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"github.com/gin-gonic/gin/render"
|
|
|
|
"html/template"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
2022-09-14 13:30:59 +00:00
|
|
|
|
2022-09-18 04:34:48 +00:00
|
|
|
//go:embed posts layout
|
2022-09-14 13:30:59 +00:00
|
|
|
var TemplateFs embed.FS
|
2022-09-18 14:06:27 +00:00
|
|
|
|
|
|
|
type FsTemplate struct {
|
|
|
|
Templates map[string]*template.Template
|
|
|
|
FuncMap template.FuncMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFsTemplate(funcMap template.FuncMap) *FsTemplate {
|
|
|
|
return &FsTemplate{FuncMap: funcMap, Templates: make(map[string]*template.Template)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FsTemplate) AddTemplate(name, main string, sub ...string) {
|
|
|
|
tmp := []string{main}
|
|
|
|
tmp = append(tmp, sub...)
|
|
|
|
t.Templates[name] = template.Must(template.New(filepath.Base(main)).Funcs(t.FuncMap).ParseFS(TemplateFs, tmp...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t FsTemplate) Instance(name string, data any) render.Render {
|
|
|
|
r := t.Templates[name]
|
|
|
|
return render.HTML{
|
|
|
|
Template: r,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|