Files
go_blog/themes/parser.go
2025-04-09 16:56:05 +08:00

35 lines
992 B
Go

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
func SwitchTheme(c *gin.Context) {
newTheme := c.PostForm("theme")
manager := &ThemeManager{} //themes.GetManager()
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"})
}
}