模板可切换,模板引擎模块化有问题
This commit is contained in:
@@ -46,25 +46,25 @@ var (
|
||||
)
|
||||
|
||||
// 核心方法:加载主题
|
||||
func (m *ThemeManager) LoadTheme(themeName string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
func (themeManager *ThemeManager) LoadTheme(themeName string) error {
|
||||
themeManager.mu.Lock()
|
||||
defer themeManager.mu.Unlock()
|
||||
|
||||
// 1. 验证主题目录结构
|
||||
themePath := filepath.Join(m.themesDir, themeName)
|
||||
themePath := filepath.Join(themeManager.themesDir, themeName)
|
||||
if !isValidTheme(themePath) {
|
||||
return fmt.Errorf("%w: %s", ErrInvalidThemeStructure, themeName)
|
||||
}
|
||||
|
||||
// 2. 加载模板文件
|
||||
tpls, err := m.parseTemplates(themePath)
|
||||
tpls, err := themeManager.parseTemplates(themePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("template parsing failed: %w", err)
|
||||
}
|
||||
|
||||
// 3. 更新当前主题
|
||||
m.currentTheme = themeName
|
||||
m.templates = tpls
|
||||
themeManager.currentTheme = themeName
|
||||
themeManager.templates = tpls
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -229,3 +229,15 @@ func (m *ThemeManager) GetAvailableThemes() ([]string, error) {
|
||||
}
|
||||
return themes, nil
|
||||
}
|
||||
|
||||
// 加载主题下所有模板(包括子模板)
|
||||
func (tm *ThemeManager) LoadTemplates(themeName string) error {
|
||||
themePath := filepath.Join("web", "themes", themeName, "templates")
|
||||
// 使用通配符加载所有tmpl文件
|
||||
tmpl, err := template.ParseGlob(filepath.Join(themePath, "*.tmpl"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载主题模板失败: %w", err)
|
||||
}
|
||||
tm.templates[themeName] = tmpl
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package themes // Declare the package at the top
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
config "go_blog/config" // Adjust import path as needed
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RenderTemplate renders a template based on the current theme
|
||||
func RenderTemplate(c *gin.Context, tmpl string, data gin.H) {
|
||||
// Get the current theme
|
||||
theme := config.GetCurrentTheme()
|
||||
|
||||
// Construct the template path: "themes/default/home.html"
|
||||
tplPath := fmt.Sprintf("themes/%s/%s", theme, tmpl)
|
||||
|
||||
// Render the template using html/template
|
||||
c.HTML(http.StatusOK, tplPath, data)
|
||||
}
|
||||
|
||||
// SwitchTheme handles POST requests to switch the current theme
|
||||
// SwitchTheme 处理主题切换请求(修正后)
|
||||
func SwitchTheme(c *gin.Context) {
|
||||
// 从上下文中获取主题管理器(通过 main.go 的 themeMiddleware 注入)
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器未找到"})
|
||||
return
|
||||
}
|
||||
themeManager, ok := tm.(*ThemeManager)
|
||||
if !ok {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"})
|
||||
return
|
||||
}
|
||||
|
||||
newTheme := c.PostForm("theme")
|
||||
if err := themeManager.LoadTheme(newTheme); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "加载主题失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
config.SetCurrentTheme(newTheme) // 持久化主题配置(需确保 config 包支持)
|
||||
c.JSON(http.StatusOK, gin.H{"status": "主题切换成功"})
|
||||
}
|
||||
Reference in New Issue
Block a user