diff --git a/route/route.go b/route/route.go index 819f0cc..7f12a64 100644 --- a/route/route.go +++ b/route/route.go @@ -25,9 +25,7 @@ func SetupRouter() *gin.Engine { return t.Format("2006年 01月 02日") }, }) - reader := templates.NewFsTemplate(r.FuncMap) - reader.AddTemplate() - r.HTMLRender = reader + r.HTMLRender = templates.NewFsTemplate(r.FuncMap).AddTemplate() r.Use(middleware.SetStaticFileCache) //gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用 /*r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{ diff --git a/templates/templatefs.go b/templates/templatefs.go index 3c680ac..33277ce 100644 --- a/templates/templatefs.go +++ b/templates/templatefs.go @@ -20,7 +20,7 @@ func NewFsTemplate(funcMap template.FuncMap) *FsTemplate { return &FsTemplate{FuncMap: funcMap, Templates: make(map[string]*template.Template)} } -func (t *FsTemplate) AddTemplate() { +func (t *FsTemplate) AddTemplate() *FsTemplate { mainTemplates, err := fs.Glob(TemplateFs, "*[^layout]/*.gohtml") if err != nil { panic(err) @@ -29,6 +29,7 @@ func (t *FsTemplate) AddTemplate() { name := filepath.Base(include) t.Templates[include] = template.Must(template.New(name).Funcs(t.FuncMap).ParseFS(TemplateFs, include, "layout/*.gohtml")) } + return t } func (t FsTemplate) Instance(name string, data any) render.Render {