diff --git a/controllers/home.go b/controllers/home.go index e281a9a..a93c799 100644 --- a/controllers/home.go +++ b/controllers/home.go @@ -44,7 +44,11 @@ func Home(c *gin.Context) { } var items []models.Content - db.Select("*").Limit(5).Find(&items, "type = ?", "post") + result := db.Select("*").Limit(5).Find(&items, "type = ?", "post") + if result.Error != nil { + c.String(http.StatusInternalServerError, "Database error: "+result.Error.Error()) + return + } tpl := themeManager.GetTemplate("index") if tpl == nil { @@ -52,9 +56,13 @@ func Home(c *gin.Context) { return } - c.Header("Content-Type", "text/html; charset=utf-8") - tpl.Execute(c.Writer, gin.H{ + data := gin.H{ "Items": items, "Title": "首页", - }) + } + + c.Header("Content-Type", "text/html; charset=utf-8") + if err := tpl.Execute(c.Writer, data); err != nil { + c.String(http.StatusInternalServerError, "Template render error: "+err.Error()) + } } diff --git a/utils/thememanager.go b/utils/thememanager.go index daa678a..dc22995 100644 --- a/utils/thememanager.go +++ b/utils/thememanager.go @@ -139,48 +139,40 @@ func fileExists(path string) (bool, error) { func (m *ThemeManager) parseTemplates(themePath string) (map[string]*template.Template, error) { templates := make(map[string]*template.Template) - // 加载公共基础模板(可选) - baseTpl := template.New("base").Funcs(template.FuncMap{ - "safeHTML": func(s string) template.HTML { return template.HTML(s) }, - }) - // 从主题目录加载模板 tplDir := filepath.Join(themePath, "templates") + + // 首先读取所有模板文件内容 + type tplFile struct { + name string + path string + content []byte + } + var tplFiles []tplFile + err := filepath.WalkDir(tplDir, func(path string, d fs.DirEntry, err error) error { if err != nil { - return err // 返回错误以停止 WalkDir + return err } if d.IsDir() { - return nil // 跳过目录 + return nil } // 支持 .html 和 .tmpl 扩展名 lowerPath := strings.ToLower(path) if !strings.HasSuffix(lowerPath, ".html") && !strings.HasSuffix(lowerPath, ".tmpl") { - return nil // 非模板文件,跳过 + return nil } - // 读取模板内容 content, err := readFile(path) if err != nil { return fmt.Errorf("failed to read template file %s: %w", path, err) } - // 生成模板名称(相对路径,不含扩展名) name := strings.TrimPrefix(path, tplDir+string(filepath.Separator)) name = strings.TrimSuffix(name, filepath.Ext(name)) - // 克隆基础模板并解析 - tpl, err := baseTpl.Clone() - if err != nil { - return fmt.Errorf("failed to clone base template for %s: %w", name, err) - } - tpl, err = tpl.Parse(string(content)) - if err != nil { - return fmt.Errorf("parse error in %s (file: %s): %w", name, path, err) - } - - templates[name] = tpl + tplFiles = append(tplFiles, tplFile{name: name, path: path, content: content}) return nil }) @@ -188,10 +180,53 @@ func (m *ThemeManager) parseTemplates(themePath string) (map[string]*template.Te return nil, err } - if len(templates) == 0 { + if len(tplFiles) == 0 { return nil, ErrNoValidTemplates } + // 创建包含所有模板的根模板 + funcMap := template.FuncMap{ + "safeHTML": func(s string) template.HTML { return template.HTML(s) }, + } + + // 首先解析基础模板(base.tmpl 和 header.tmpl) + var baseContent strings.Builder + for _, tf := range tplFiles { + if tf.name == "base" || tf.name == "header" { + baseContent.WriteString(string(tf.content)) + baseContent.WriteString("\n") + } + } + + rootTpl, err := template.New("root").Funcs(funcMap).Parse(baseContent.String()) + if err != nil { + return nil, fmt.Errorf("failed to parse base templates: %w", err) + } + + // 为每个页面模板单独解析,继承基础模板 + for _, tf := range tplFiles { + // 跳过基础模板,它们已经在 rootTpl 中 + if tf.name == "base" || tf.name == "header" { + templates[tf.name] = rootTpl + continue + } + + // 克隆基础模板并添加页面特定内容 + tpl, err := rootTpl.Clone() + if err != nil { + return nil, fmt.Errorf("failed to clone base template for %s: %w", tf.name, err) + } + + // 解析页面特定内容,使用页面名称作为 define 名称 + pageContent := string(tf.content) + _, err = tpl.Parse(pageContent) + if err != nil { + return nil, fmt.Errorf("failed to parse template %s: %w", tf.name, err) + } + + templates[tf.name] = tpl + } + return templates, nil } diff --git a/utils/utils.go b/utils/utils.go index a69b6d6..8eaadae 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,13 +1,12 @@ package utils // 示例:插件接口 import ( - "go_blog/models" "strings" "github.com/gin-gonic/gin" ) type Plugin interface { - OnArticleCreate(article *models.Article) // 文章创建钩子 + //OnArticleCreate(article *models.Article) // 文章创建钩子 RegisterRoutes(r *gin.Engine) // 添加新路由 } diff --git a/web/themes/default/templates/base.tmpl b/web/themes/default/templates/base.tmpl index 2a445ed..c6d6098 100644 --- a/web/themes/default/templates/base.tmpl +++ b/web/themes/default/templates/base.tmpl @@ -1,138 +1,27 @@ - +{{ define "base" }} - {{ .Title}} + {{ .Title }} - - {{ template "header" . }} - -
{{ block "content" . }}{{ end }}
- - - - - +
+
{{ block "content" . }}{{ end }}
- - \ No newline at end of file + +{{ end }} \ No newline at end of file diff --git a/web/themes/default/templates/index.tmpl b/web/themes/default/templates/index.tmpl index 537bb2d..d218e50 100644 --- a/web/themes/default/templates/index.tmpl +++ b/web/themes/default/templates/index.tmpl @@ -1,102 +1,23 @@ - {{ define "content" }} -
- -
- {{ range .Items }} -
-
-

Title: {{ .Title }}

-
-

Slug: {{ .Slug }}

-

Text: {{ .Text }}

-
Created: {{ .Created }},
-
-
- {{ end }} - - -
- +
+ {{ range .Items }} +
+ +

{{ .Title }}

+
+

{{ .Text }}

+ 创建于: {{ .Created }}
- + {{ else }} +
+

暂无文章

+
+ {{ end }} +
+ {{ end }} - -{{ template "base.tmpl" . }} \ No newline at end of file +{{ template "base" . }} \ No newline at end of file diff --git a/web/themes/default/templates/post.tmpl b/web/themes/default/templates/post.tmpl index 9b53295..44f269a 100644 --- a/web/themes/default/templates/post.tmpl +++ b/web/themes/default/templates/post.tmpl @@ -1,64 +1,29 @@ - - - - Page 1 - - - - - - -
-
-
-

{{.Title}}

-
- {{.AuthorID}} - {{.Created}} - {{.CommentsNum}}条 -
-
-
- {{.Text}} -
-
- 标签:{{.Type}} -
-
-
-

非特殊说明,本博所有文章均为博主原创。

- -
-
-
-
- 上一篇 -
-
- 下一篇 -
-
-
-
+{{ define "content" }} +
+
+

{{ .Title }}

+
+ 作者: {{ .AuthorID }} | + 时间: {{ .Created }} | + 评论: {{ .CommentsNum }}
+
+
+ {{ .Text }} +
+
+ 标签: {{ .Type }} +
+
+ +
- - + +{{ end }} + +{{ template "base" . }}