模板可切换,模板引擎模块化有问题
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user