Templates mit HTMX
This commit is contained in:
@@ -2,9 +2,9 @@ package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"xcpq_editor/internal/handlers"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -31,7 +31,13 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
|
||||
// static content
|
||||
router.Static("/static", "../static")
|
||||
router.GET("/", home)
|
||||
router.GET("/", handlers.Home)
|
||||
router.GET("/properties", handlers.PropertiesHandler)
|
||||
/*
|
||||
router.GET("/products", handlers.ProductsHandler)
|
||||
router.GET("/conditions", handlers.ConditionsHandler)
|
||||
router.GET("/translations", handlers.TranslationsHandler)
|
||||
*/
|
||||
|
||||
router.GET("/test", Test)
|
||||
|
||||
@@ -55,38 +61,3 @@ func Test(ctx *gin.Context) {
|
||||
"message": "Test",
|
||||
})
|
||||
}
|
||||
|
||||
func home(ctx *gin.Context) {
|
||||
|
||||
/* load templates */
|
||||
|
||||
tmpl, err := template.ParseFiles(
|
||||
"../templates/pages/index.html",
|
||||
"../templates/layouts/navbar.html",
|
||||
"../templates/layouts/properties.html",
|
||||
"../templates/layouts/footer.html")
|
||||
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
renderTemplate(ctx, tmpl, nil)
|
||||
}
|
||||
|
||||
func renderTemplate(ctx *gin.Context, tmpl *template.Template, data interface{}) {
|
||||
if tmpl == nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "template is nil",
|
||||
})
|
||||
return
|
||||
}
|
||||
err := tmpl.Execute(ctx.Writer, data)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user