wp-go/multipTemplate/multiptemplate.go

117 lines
3.0 KiB
Go
Raw Normal View History

2023-01-21 06:49:24 +00:00
package multipTemplate
import (
"embed"
"github.com/gin-gonic/gin/render"
"html/template"
"io/fs"
"path/filepath"
)
type MultipleFileTemplate struct {
Template maps
2023-01-21 06:49:24 +00:00
}
type MultipleFsTemplate struct {
MultipleFileTemplate
Fs embed.FS
}
type TemplateMaps map[string]*template.Template
func (m TemplateMaps) Load(name string) (*template.Template, bool) {
v, ok := m[name]
return v, ok
}
func (m TemplateMaps) Store(name string, v *template.Template) {
m[name] = v
}
type maps interface {
Load(name string) (*template.Template, bool)
Store(name string, v *template.Template)
}
2023-01-23 15:42:37 +00:00
func (t *MultipleFileTemplate) AppendTemplate(name string, templates ...string) *MultipleFileTemplate {
tmpl, ok := t.Template.Load(name)
2023-01-23 15:42:37 +00:00
if ok {
t.Template.Store(name, template.Must(tmpl.ParseFiles(templates...)))
2023-01-23 15:42:37 +00:00
}
return t
}
func (t *MultipleFsTemplate) AppendTemplate(name string, templates ...string) *MultipleFsTemplate {
tmpl, ok := t.Template.Load(name)
2023-01-23 15:42:37 +00:00
if ok {
t.Template.Store(name, template.Must(tmpl.ParseFS(t.Fs, templates...)))
2023-01-23 15:42:37 +00:00
}
return t
}
func NewFileTemplates(m maps) *MultipleFileTemplate {
2023-01-21 06:49:24 +00:00
return &MultipleFileTemplate{
Template: m,
2023-01-21 06:49:24 +00:00
}
}
func NewFsTemplate(f embed.FS) *MultipleFsTemplate {
return &MultipleFsTemplate{
MultipleFileTemplate: MultipleFileTemplate{
Template: TemplateMaps(make(map[string]*template.Template)),
},
Fs: f,
}
}
func NewFsTemplates(f embed.FS, m maps) *MultipleFsTemplate {
return &MultipleFsTemplate{
MultipleFileTemplate: MultipleFileTemplate{
Template: m,
2023-01-21 06:49:24 +00:00
},
Fs: f,
}
}
func (t *MultipleFileTemplate) SetTemplate(name string, templ *template.Template) *MultipleFileTemplate {
if _, ok := t.Template.Load(name); ok {
2023-01-21 06:49:24 +00:00
panic("exists same template " + name)
}
t.Template.Store(name, templ)
2023-01-21 06:49:24 +00:00
return t
}
func (t *MultipleFileTemplate) AddTemplate(mainTemplatePattern string, fnMap template.FuncMap, layoutTemplatePattern ...string) *MultipleFileTemplate {
mainTemplates, err := filepath.Glob(mainTemplatePattern)
if err != nil {
panic(err)
}
for _, mainTemplate := range mainTemplates {
file := filepath.Base(mainTemplate)
pattern := append([]string{mainTemplate}, layoutTemplatePattern...)
t.Template.Store(mainTemplate, template.Must(template.New(file).Funcs(fnMap).ParseFiles(pattern...)))
2023-01-21 06:49:24 +00:00
}
return t
}
func (t *MultipleFileTemplate) Instance(name string, data any) render.Render {
v, _ := t.Template.Load(name)
2023-01-21 06:49:24 +00:00
return render.HTML{
Template: v,
2023-01-21 06:49:24 +00:00
Data: data,
}
}
func (t *MultipleFsTemplate) AddTemplate(mainTemplatePattern string, fnMap template.FuncMap, layoutTemplatePattern ...string) *MultipleFsTemplate {
mainTemplates, err := fs.Glob(t.Fs, mainTemplatePattern)
if err != nil {
panic(err)
}
for _, mainTemplate := range mainTemplates {
if _, ok := t.Template.Load(mainTemplate); ok {
2023-01-21 06:49:24 +00:00
panic("exists same Template " + mainTemplate)
}
file := filepath.Base(mainTemplate)
pattern := append([]string{mainTemplate}, layoutTemplatePattern...)
t.Template.Store(mainTemplate, template.Must(template.New(file).Funcs(fnMap).ParseFS(t.Fs, pattern...)))
2023-01-21 06:49:24 +00:00
}
return t
}