模板查询调整

This commit is contained in:
xing 2023-01-20 19:58:45 +08:00
parent cf6bf03d33
commit 13dbc1ab81
5 changed files with 34 additions and 15 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -48,7 +49,7 @@ func Detail(c *gin.Context) {
if isApproveComment == true { if isApproveComment == true {
return 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 := c.Param("id")
Id := 0 Id := 0

View File

@ -203,7 +203,7 @@ func Index(c *gin.Context) {
stat = http.StatusInternalServerError stat = http.StatusInternalServerError
return return
} }
t := getTemplateName() t := theme.GetTemplateName()
theme.Hook(t, stat, c, ginH, int(h.scene)) theme.Hook(t, stat, c, ginH, int(h.scene))
}() }()
err = h.parseParams() err = h.parseParams()
@ -256,11 +256,3 @@ func Index(c *gin.Context) {
ginH["title"] = h.getTitle() ginH["title"] = h.getTitle()
ginH["pagination"] = pagination.NewParsePagination(totalRaw, h.pageSize, h.page, h.paginationStep, q, c.Request.URL.Path) 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
}

View File

@ -37,6 +37,7 @@ type Config struct {
PostCommentUrl string `yaml:"postCommentUrl"` PostCommentUrl string `yaml:"postCommentUrl"`
TrustIps []string `yaml:"trustIps"` TrustIps []string `yaml:"trustIps"`
TrustServerNames []string `yaml:"trustServerNames"` TrustServerNames []string `yaml:"trustServerNames"`
Theme string `yaml:"theme"`
} }
type Mail struct { type Mail struct {

View File

@ -12,13 +12,16 @@ import (
//go:embed *[^.go] //go:embed *[^.go]
var TemplateFs embed.FS var TemplateFs embed.FS
var templates map[string]*template.Template
type FsTemplate struct { type FsTemplate struct {
Templates map[string]*template.Template Templates map[string]*template.Template
FuncMap template.FuncMap FuncMap template.FuncMap
} }
func NewFsTemplate(funcMap template.FuncMap) *FsTemplate { 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 { 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) arr, err := TemplateFs.ReadDir(tml)
if err != nil { if err != nil {
return return false
} }
if len(arr) > 0 { 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]
} }

View File

@ -1,9 +1,22 @@
package theme package theme
import ( import (
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/theme/twentyseventeen" "github.com/fthvgb1/wp-go/internal/theme/twentyseventeen"
"github.com/fthvgb1/wp-go/internal/wpconfig"
) )
func InitThemeAndTemplateFuncMap() { func InitThemeAndTemplateFuncMap() {
AddThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook) 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
}