From 4676bfac93e95d119d4312a437beef08f4e7135f Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 19 Sep 2022 12:44:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- route/route.go | 4 +--- templates/templatefs.go | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) 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 {