optimize code

This commit is contained in:
xing 2024-01-24 00:25:37 +08:00
parent 20ea36c727
commit 802c7c7dcc
2 changed files with 2 additions and 6 deletions

View File

@ -26,7 +26,6 @@ func BuildTemplate() *multipTemplate.MultipleFsTemplate {
} }
} else { } else {
multiple = multipTemplate.NewFsTemplates(TemplateFs, templates) multiple = multipTemplate.NewFsTemplates(TemplateFs, templates)
multiple.FuncMap = FuncMap()
commonTemplate(multiple) commonTemplate(multiple)
} }
@ -53,10 +52,11 @@ func commonTemplate(t *multipTemplate.MultipleFsTemplate) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
funMap := FuncMap()
for _, main := range m { for _, main := range m {
file := filepath.Base(main) file := filepath.Base(main)
dir := strings.Split(main, "/")[0] dir := strings.Split(main, "/")[0]
templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml")) templ := template.Must(template.New(file).Funcs(funMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml"))
t.SetTemplate(main, templ) t.SetTemplate(main, templ)
} }
} }

View File

@ -10,7 +10,6 @@ import (
type MultipleFileTemplate struct { type MultipleFileTemplate struct {
Template maps Template maps
FuncMap template.FuncMap
} }
type MultipleFsTemplate struct { type MultipleFsTemplate struct {
MultipleFileTemplate MultipleFileTemplate
@ -52,14 +51,12 @@ func (t *MultipleFsTemplate) AppendTemplate(name string, templates ...string) *M
func NewFileTemplates(m maps) *MultipleFileTemplate { func NewFileTemplates(m maps) *MultipleFileTemplate {
return &MultipleFileTemplate{ return &MultipleFileTemplate{
Template: m, Template: m,
FuncMap: make(template.FuncMap),
} }
} }
func NewFsTemplate(f embed.FS) *MultipleFsTemplate { func NewFsTemplate(f embed.FS) *MultipleFsTemplate {
return &MultipleFsTemplate{ return &MultipleFsTemplate{
MultipleFileTemplate: MultipleFileTemplate{ MultipleFileTemplate: MultipleFileTemplate{
Template: TemplateMaps(make(map[string]*template.Template)), Template: TemplateMaps(make(map[string]*template.Template)),
FuncMap: make(template.FuncMap),
}, },
Fs: f, Fs: f,
} }
@ -68,7 +65,6 @@ func NewFsTemplates(f embed.FS, m maps) *MultipleFsTemplate {
return &MultipleFsTemplate{ return &MultipleFsTemplate{
MultipleFileTemplate: MultipleFileTemplate{ MultipleFileTemplate: MultipleFileTemplate{
Template: m, Template: m,
FuncMap: make(template.FuncMap),
}, },
Fs: f, Fs: f,
} }