模板查询调整

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/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

View File

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

View File

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

View File

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

View File

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