From 13dbc1ab8139c8bb54d1c91892ad2f0bf710b248 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 20 Jan 2023 19:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E6=9F=A5=E8=AF=A2=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/actions/detail.go | 3 ++- internal/actions/index.go | 10 +--------- internal/pkg/config/config.go | 1 + internal/theme/fs.go | 22 +++++++++++++++++----- internal/theme/theme.go | 13 +++++++++++++ 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/internal/actions/detail.go b/internal/actions/detail.go index 5e95d63..3af8404 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -7,6 +7,7 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/plugins" + "github.com/fthvgb1/wp-go/internal/theme" "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" @@ -48,7 +49,7 @@ func Detail(c *gin.Context) { if isApproveComment == true { return } - c.HTML(status, helper.StrJoin(getTemplateName(), "/posts/detail.gohtml"), h) + c.HTML(status, helper.StrJoin(theme.GetTemplateName(), "/posts/detail.gohtml"), h) }() id := c.Param("id") Id := 0 diff --git a/internal/actions/index.go b/internal/actions/index.go index e5e2bfb..60e570c 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -203,7 +203,7 @@ func Index(c *gin.Context) { stat = http.StatusInternalServerError return } - t := getTemplateName() + t := theme.GetTemplateName() theme.Hook(t, stat, c, ginH, int(h.scene)) }() err = h.parseParams() @@ -256,11 +256,3 @@ func Index(c *gin.Context) { ginH["title"] = h.getTitle() ginH["pagination"] = pagination.NewParsePagination(totalRaw, h.pageSize, h.page, h.paginationStep, q, c.Request.URL.Path) } - -func getTemplateName() string { - tmlp := wpconfig.Options.Value("template") - if i, err := theme.IsTemplateIsExist(tmlp); err != nil || !i { - tmlp = "twentyfifteen" - } - return tmlp -} diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index d3370d0..aafb293 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -37,6 +37,7 @@ type Config struct { PostCommentUrl string `yaml:"postCommentUrl"` TrustIps []string `yaml:"trustIps"` TrustServerNames []string `yaml:"trustServerNames"` + Theme string `yaml:"theme"` } type Mail struct { diff --git a/internal/theme/fs.go b/internal/theme/fs.go index d0dbe7e..0ed96d3 100644 --- a/internal/theme/fs.go +++ b/internal/theme/fs.go @@ -12,13 +12,16 @@ import ( //go:embed *[^.go] var TemplateFs embed.FS +var templates map[string]*template.Template + type FsTemplate struct { Templates map[string]*template.Template FuncMap template.FuncMap } func NewFsTemplate(funcMap template.FuncMap) *FsTemplate { - return &FsTemplate{FuncMap: funcMap, Templates: make(map[string]*template.Template)} + templates = make(map[string]*template.Template) + return &FsTemplate{FuncMap: funcMap, Templates: templates} } func (t FsTemplate) SetTemplate() *FsTemplate { @@ -43,13 +46,22 @@ func (t FsTemplate) Instance(name string, data any) render.Render { } } -func IsTemplateIsExist(tml string) (r bool, err error) { +func IsTemplateDirExists(tml string) bool { arr, err := TemplateFs.ReadDir(tml) if err != nil { - return + return false } if len(arr) > 0 { - r = true + return true } - return + return false +} + +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/theme.go b/internal/theme/theme.go index 2efb901..67bcd78 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -1,9 +1,22 @@ package theme import ( + "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/theme/twentyseventeen" + "github.com/fthvgb1/wp-go/internal/wpconfig" ) func InitThemeAndTemplateFuncMap() { AddThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook) } + +func GetTemplateName() string { + tmlp := config.Conf.Load().Theme + if tmlp == "" { + tmlp = wpconfig.Options.Value("template") + } + if !IsTemplateDirExists(tmlp) { + tmlp = "twentyfifteen" + } + return tmlp +}