去年默认配置。

This commit is contained in:
2026-07-22 18:02:05 +08:00
parent aab3aeb5b2
commit 8d0b75e484
7 changed files with 43 additions and 76 deletions
+30 -2
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"html/template"
"net/http"
"path/filepath"
"sync"
"github.com/gin-gonic/gin/render"
@@ -84,16 +85,43 @@ func (r *TemplateRenderer) Reload(theme string) {
func (r *TemplateRenderer) reload(theme string) {
templateDir := "templates/" + theme
// 需要独立渲染的页面模板
pages := []string{"index", "post", "page"}
// 收集主题目录下的公共局部模板(例如 sidebar/header/footer),
// 这些模板通过 {{template "xxx" .}} 被 base.html 引用,
// 必须与页面模板一起解析,否则渲染时会因模板未定义而失败(白屏)。
partials := []string{}
if matches, err := filepath.Glob(templateDir + "/*.html"); err == nil {
for _, f := range matches {
base := filepath.Base(f)
if base == "base.html" {
continue
}
name := base[:len(base)-len(".html")]
isPage := false
for _, p := range pages {
if p == name {
isPage = true
break
}
}
if !isPage {
partials = append(partials, f)
}
}
}
// 为每个页面创建独立的模板组合
pages := []string{"index", "post", "page", "sidebar"}
newTemplates := make(map[string]*template.Template)
for _, pageName := range pages {
// 只加载 base.html 和对应的页面模板
// 加载 base.html、对应页面模板以及所有公共局部模板
allFiles := []string{
templateDir + "/base.html",
templateDir + "/" + pageName + ".html",
}
allFiles = append(allFiles, partials...)
// 解析模板文件
tmpl, err := template.New("").Funcs(templateFuncs()).ParseFiles(allFiles...)