2025-04-09 16:56:05 +08:00
|
|
|
package themes // Declare the package at the top
|
2025-04-01 17:59:10 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-04-09 16:56:05 +08:00
|
|
|
config "go_blog/config" // Adjust import path as needed
|
2025-04-01 17:59:10 +08:00
|
|
|
"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
|
|
|
|
|
func SwitchTheme(c *gin.Context) {
|
|
|
|
|
newTheme := c.PostForm("theme")
|
2025-04-09 16:56:05 +08:00
|
|
|
manager := &ThemeManager{} //themes.GetManager()
|
2025-04-01 17:59:10 +08:00
|
|
|
|
|
|
|
|
if err := manager.LoadTheme(newTheme); err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
|
} else {
|
|
|
|
|
config.SetCurrentTheme(newTheme) // Update the configuration
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
|
|
|
|
}
|
|
|
|
|
}
|