Templates mit HTMX

This commit is contained in:
2026-09-09 21:31:08 +02:00
parent 870a3cf708
commit d1e210d03e
8 changed files with 63 additions and 45 deletions
@@ -2,9 +2,9 @@ package server
import ( import (
"database/sql" "database/sql"
"html/template"
"log" "log"
"net/http" "net/http"
"xcpq_editor/internal/handlers"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -31,7 +31,13 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
// static content // static content
router.Static("/static", "../static") 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) router.GET("/test", Test)
@@ -55,38 +61,3 @@ func Test(ctx *gin.Context) {
"message": "Test", "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(),
})
}
}
@@ -0,0 +1,48 @@
package handlers
import (
"html/template"
"net/http"
"github.com/gin-gonic/gin"
)
func Home(ctx *gin.Context) {
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
}
err = tmpl.Execute(ctx.Writer, nil)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
}
func PropertiesHandler(ctx *gin.Context) {
ptmpl, err := template.ParseFiles(
"../templates/layouts/properties.html")
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
err = ptmpl.Execute(ctx.Writer, nil)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
}
@@ -6,10 +6,10 @@
<nav> <nav>
<!-- Navigation links can be added here --> <!-- Navigation links can be added here -->
<ul> <ul>
<li><a href="#">Eigenschaften</a></li> <li><a href="#" hx-get="/properties" hx-target="#main-content">Eigenschaften</a></li>
<li><a href="#">Produkte</a></li> <li><a href="#" hx-get="/products" hx-target="#main-content">Produkte</a></li>
<li><a href="#">Konditionen</a></li> <li><a href="#" hx-get="/conditions" hx-target="#main-content">Konditionen</a></li>
<li><a href="#">Übersetzungen</a></li> <li><a href="#" hx-get="/translations" hx-target="#main-content">Übersetzungen</a></li>
<li><a href="#">Datenimporte</a></li> <li><a href="#">Datenimporte</a></li>
</ul> </ul>
</nav> </nav>
@@ -1,4 +1,4 @@
{{define "properties"}}
<!-- Properties content can be added here --> <!-- Properties content can be added here -->
<h2>X-CPQ Material Editor: Eigenschaften</h2> <h2>X-CPQ Material Editor: Eigenschaften</h2>
<!-- Main content can be added here --> <!-- Main content can be added here -->
@@ -79,4 +79,3 @@
</div> </div>
{{end}}
@@ -21,7 +21,7 @@
</header> </header>
<main> <main>
<section id="main-content"> <section id="main-content">
{{ template "properties" . }} <h1>Bitte einen Bereich in der Navigation auswählen</h1>
</section> </section>
</main> </main>