模板可切换,模板引擎模块化有问题

This commit is contained in:
张超
2025-06-19 22:27:06 +08:00
parent 4c9f6c140f
commit 8241b8c61f
16 changed files with 401 additions and 122 deletions

View File

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