把多模板抽象出来

This commit is contained in:
xing 2023-01-21 14:49:24 +08:00
parent 13dbc1ab81
commit 2188619755
4 changed files with 105 additions and 35 deletions

View File

@ -26,7 +26,8 @@ func SetupRouter() (*gin.Engine, func()) {
} }
} }
r.HTMLRender = theme.NewFsTemplate(theme.FuncMap()).SetTemplate() r.HTMLRender = theme.GetTemplate()
validServerName, reloadValidServerNameFn := middleware.ValidateServerNames() validServerName, reloadValidServerNameFn := middleware.ValidateServerNames()
fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime) fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
r.Use( r.Use(

View File

@ -2,7 +2,7 @@ package theme
import ( import (
"embed" "embed"
"github.com/gin-gonic/gin/render" "github.com/fthvgb1/wp-go/multipTemplate"
"html/template" "html/template"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
@ -12,37 +12,29 @@ import (
//go:embed *[^.go] //go:embed *[^.go]
var TemplateFs embed.FS var TemplateFs embed.FS
var templates map[string]*template.Template var templates map[string]*template.Template //方便外部获取模板render后的字符串不然在gin中获取不了
type FsTemplate struct { func GetTemplate() *multipTemplate.MultipleFsTemplate {
Templates map[string]*template.Template t := multipTemplate.NewFsTemplate(TemplateFs)
FuncMap template.FuncMap 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) func commonTemplate(t *multipTemplate.MultipleFsTemplate) {
return &FsTemplate{FuncMap: funcMap, Templates: templates} m, err := fs.Glob(t.Fs, "*/*[^layout]/*.gohtml")
}
func (t FsTemplate) SetTemplate() *FsTemplate {
mainTemplates, err := fs.Glob(TemplateFs, `*/*[^layout]/*.gohtml`)
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, include := range mainTemplates { for _, main := range m {
name := filepath.Base(include) file := filepath.Base(main)
c := strings.Split(include, "/") dir := strings.Split(main, "/")[0]
base := c[0] templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml")))
t.Templates[include] = template.Must(template.New(name).Funcs(t.FuncMap).ParseFS(TemplateFs, include, filepath.Join(base, "layout/*.gohtml"))) t.SetTemplate(main, templ)
}
return &t
}
func (t FsTemplate) Instance(name string, data any) render.Render {
r := t.Templates[name]
return render.HTML{
Template: r,
Data: data,
} }
} }
@ -61,7 +53,3 @@ func IsTemplateExists(tml string) bool {
t, ok := templates[tml] t, ok := templates[tml]
return ok && t != nil return ok && t != nil
} }
func GetTemplate(tml string) *template.Template {
return templates[tml]
}

View File

@ -6,7 +6,7 @@ import (
"time" "time"
) )
var funcs = template.FuncMap{ var comFn = template.FuncMap{
"unescaped": func(s string) any { "unescaped": func(s string) any {
return template.HTML(s) return template.HTML(s)
}, },
@ -20,12 +20,12 @@ var funcs = template.FuncMap{
} }
func FuncMap() template.FuncMap { func FuncMap() template.FuncMap {
return funcs return comFn
} }
func AddTemplateFunc(fnName string, fn any) { func AddTemplateFunc(fnName string, fn any) {
if _, ok := funcs[fnName]; ok { if _, ok := comFn[fnName]; ok {
panic("exists same name func") panic("exists same name func")
} }
funcs[fnName] = fn comFn[fnName] = fn
} }

View File

@ -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
}