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 {
|
2024-01-23 14:59:15 +00:00
|
|
|
Template maps
|
2023-01-21 06:49:24 +00:00
|
|
|
FuncMap template.FuncMap
|
|
|
|
}
|
|
|
|
type MultipleFsTemplate struct {
|
|
|
|
MultipleFileTemplate
|
|
|
|
Fs embed.FS
|
|
|
|
}
|
|
|
|
|
2024-01-23 14:59:15 +00:00
|
|
|
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 {
|
2024-01-23 14:59:15 +00:00
|
|
|
tmpl, ok := t.Template.Load(name)
|
2023-01-23 15:42:37 +00:00
|
|
|
if ok {
|
2024-01-23 14:59:15 +00:00
|
|
|
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 {
|
2024-01-23 14:59:15 +00:00
|
|
|
tmpl, ok := t.Template.Load(name)
|
2023-01-23 15:42:37 +00:00
|
|
|
if ok {
|
2024-01-23 14:59:15 +00:00
|
|
|
t.Template.Store(name, template.Must(tmpl.ParseFS(t.Fs, templates...)))
|
2023-01-23 15:42:37 +00:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2024-01-23 14:59:15 +00:00
|
|
|
func NewFileTemplates(m maps) *MultipleFileTemplate {
|
2023-01-21 06:49:24 +00:00
|
|
|
return &MultipleFileTemplate{
|
2024-01-23 14:59:15 +00:00
|
|
|
Template: m,
|
2023-01-21 06:49:24 +00:00
|
|
|
FuncMap: make(template.FuncMap),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func NewFsTemplate(f embed.FS) *MultipleFsTemplate {
|
|
|
|
return &MultipleFsTemplate{
|
|
|
|
MultipleFileTemplate: MultipleFileTemplate{
|
2024-01-23 14:59:15 +00:00
|
|
|
Template: TemplateMaps(make(map[string]*template.Template)),
|
|
|
|
FuncMap: make(template.FuncMap),
|
|
|
|
},
|
|
|
|
Fs: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func NewFsTemplates(f embed.FS, m maps) *MultipleFsTemplate {
|
|
|
|
return &MultipleFsTemplate{
|
|
|
|
MultipleFileTemplate: MultipleFileTemplate{
|
|
|
|
Template: m,
|
2023-01-21 06:49:24 +00:00
|
|
|
FuncMap: make(template.FuncMap),
|
|
|
|
},
|
|
|
|
Fs: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *MultipleFileTemplate) SetTemplate(name string, templ *template.Template) *MultipleFileTemplate {
|
2024-01-23 14:59:15 +00:00
|
|
|
if _, ok := t.Template.Load(name); ok {
|
2023-01-21 06:49:24 +00:00
|
|
|
panic("exists same template " + name)
|
|
|
|
}
|
2024-01-23 14:59:15 +00:00
|
|
|
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...)
|
2024-01-23 14:59:15 +00:00
|
|
|
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 {
|
2024-01-23 14:59:15 +00:00
|
|
|
v, _ := t.Template.Load(name)
|
2023-01-21 06:49:24 +00:00
|
|
|
return render.HTML{
|
2024-01-23 14:59:15 +00:00
|
|
|
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 {
|
2024-01-23 14:59:15 +00:00
|
|
|
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...)
|
2024-01-23 14:59:15 +00:00
|
|
|
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
|
|
|
|
}
|