diff --git a/internal/cmd/route/route.go b/internal/cmd/route/route.go index 052eec5..7eda3d0 100644 --- a/internal/cmd/route/route.go +++ b/internal/cmd/route/route.go @@ -26,7 +26,8 @@ func SetupRouter() (*gin.Engine, func()) { } } - r.HTMLRender = theme.NewFsTemplate(theme.FuncMap()).SetTemplate() + r.HTMLRender = theme.GetTemplate() + validServerName, reloadValidServerNameFn := middleware.ValidateServerNames() fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime) r.Use( diff --git a/internal/theme/fs.go b/internal/theme/fs.go index 0ed96d3..b4e5e67 100644 --- a/internal/theme/fs.go +++ b/internal/theme/fs.go @@ -2,7 +2,7 @@ package theme import ( "embed" - "github.com/gin-gonic/gin/render" + "github.com/fthvgb1/wp-go/multipTemplate" "html/template" "io/fs" "path/filepath" @@ -12,37 +12,29 @@ import ( //go:embed *[^.go] var TemplateFs embed.FS -var templates map[string]*template.Template +var templates map[string]*template.Template //方便外部获取模板render后的字符串,不然在gin中获取不了 -type FsTemplate struct { - Templates map[string]*template.Template - FuncMap template.FuncMap +func GetTemplate() *multipTemplate.MultipleFsTemplate { + t := multipTemplate.NewFsTemplate(TemplateFs) + templates = t.Template + t.FuncMap = FuncMap() + commonTemplate(t) + /*t.AddTemplate("twentyfifteen/*[^layout]/*.gohtml", FuncMap(), "twentyfifteen/layout/*.gohtml"). //单个主题设置 + AddTemplate("twentyseventeen/*[^layout]/*.gohtml", FuncMap(), "twentyseventeen/layout/*.gohtml")*/ + return t } -func NewFsTemplate(funcMap template.FuncMap) *FsTemplate { - templates = make(map[string]*template.Template) - return &FsTemplate{FuncMap: funcMap, Templates: templates} -} - -func (t FsTemplate) SetTemplate() *FsTemplate { - mainTemplates, err := fs.Glob(TemplateFs, `*/*[^layout]/*.gohtml`) +// 所有主题模板通用设置 +func commonTemplate(t *multipTemplate.MultipleFsTemplate) { + m, err := fs.Glob(t.Fs, "*/*[^layout]/*.gohtml") if err != nil { panic(err) } - for _, include := range mainTemplates { - name := filepath.Base(include) - c := strings.Split(include, "/") - base := c[0] - t.Templates[include] = template.Must(template.New(name).Funcs(t.FuncMap).ParseFS(TemplateFs, include, filepath.Join(base, "layout/*.gohtml"))) - } - return &t -} - -func (t FsTemplate) Instance(name string, data any) render.Render { - r := t.Templates[name] - return render.HTML{ - Template: r, - Data: data, + for _, main := range m { + file := filepath.Base(main) + dir := strings.Split(main, "/")[0] + templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"))) + t.SetTemplate(main, templ) } } @@ -61,7 +53,3 @@ func IsTemplateExists(tml string) bool { t, ok := templates[tml] return ok && t != nil } - -func GetTemplate(tml string) *template.Template { - return templates[tml] -} diff --git a/internal/theme/templateFuncs.go b/internal/theme/templateFuncs.go index d5f2d2b..2c7d0a6 100644 --- a/internal/theme/templateFuncs.go +++ b/internal/theme/templateFuncs.go @@ -6,7 +6,7 @@ import ( "time" ) -var funcs = template.FuncMap{ +var comFn = template.FuncMap{ "unescaped": func(s string) any { return template.HTML(s) }, @@ -20,12 +20,12 @@ var funcs = template.FuncMap{ } func FuncMap() template.FuncMap { - return funcs + return comFn } func AddTemplateFunc(fnName string, fn any) { - if _, ok := funcs[fnName]; ok { + if _, ok := comFn[fnName]; ok { panic("exists same name func") } - funcs[fnName] = fn + comFn[fnName] = fn } diff --git a/multipTemplate/multiptemplate.go b/multipTemplate/multiptemplate.go new file mode 100644 index 0000000..07dd545 --- /dev/null +++ b/multipTemplate/multiptemplate.go @@ -0,0 +1,81 @@ +package multipTemplate + +import ( + "embed" + "github.com/gin-gonic/gin/render" + "html/template" + "io/fs" + "path/filepath" +) + +type MultipleFileTemplate struct { + Template map[string]*template.Template + FuncMap template.FuncMap +} +type MultipleFsTemplate struct { + MultipleFileTemplate + Fs embed.FS +} + +func NewFileTemplate() *MultipleFileTemplate { + return &MultipleFileTemplate{ + Template: make(map[string]*template.Template), + FuncMap: make(template.FuncMap), + } +} +func NewFsTemplate(f embed.FS) *MultipleFsTemplate { + return &MultipleFsTemplate{ + MultipleFileTemplate: MultipleFileTemplate{ + Template: make(map[string]*template.Template), + FuncMap: make(template.FuncMap), + }, + Fs: f, + } +} + +func (t *MultipleFileTemplate) SetTemplate(name string, templ *template.Template) *MultipleFileTemplate { + if _, ok := t.Template[name]; ok { + panic("exists same template " + name) + } + t.Template[name] = templ + 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 { + if _, ok := t.Template[mainTemplate]; ok { + panic("exists same Template " + mainTemplate) + } + file := filepath.Base(mainTemplate) + pattern := append([]string{mainTemplate}, layoutTemplatePattern...) + t.Template[mainTemplate] = template.Must(template.New(file).Funcs(fnMap).ParseFiles(pattern...)) + } + return t +} + +func (t *MultipleFileTemplate) Instance(name string, data any) render.Render { + return render.HTML{ + Template: t.Template[name], + 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[mainTemplate]; ok { + panic("exists same Template " + mainTemplate) + } + file := filepath.Base(mainTemplate) + pattern := append([]string{mainTemplate}, layoutTemplatePattern...) + t.Template[mainTemplate] = template.Must(template.New(file).Funcs(fnMap).ParseFS(t.Fs, pattern...)) + } + return t +}