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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
<!-- Navigation links can be added here -->
|
||||
<ul>
|
||||
<li><a href="#">Eigenschaften</a></li>
|
||||
<li><a href="#">Produkte</a></li>
|
||||
<li><a href="#">Konditionen</a></li>
|
||||
<li><a href="#">Übersetzungen</a></li>
|
||||
<li><a href="#" hx-get="/properties" hx-target="#main-content">Eigenschaften</a></li>
|
||||
<li><a href="#" hx-get="/products" hx-target="#main-content">Produkte</a></li>
|
||||
<li><a href="#" hx-get="/conditions" hx-target="#main-content">Konditionen</a></li>
|
||||
<li><a href="#" hx-get="/translations" hx-target="#main-content">Übersetzungen</a></li>
|
||||
<li><a href="#">Datenimporte</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{{define "properties"}}
|
||||
|
||||
<!-- Properties content can be added here -->
|
||||
<h2>X-CPQ Material Editor: Eigenschaften</h2>
|
||||
<!-- Main content can be added here -->
|
||||
@@ -79,4 +79,3 @@
|
||||
</div>
|
||||
|
||||
|
||||
{{end}}
|
||||
@@ -21,7 +21,7 @@
|
||||
</header>
|
||||
<main>
|
||||
<section id="main-content">
|
||||
{{ template "properties" . }}
|
||||
<h1>Bitte einen Bereich in der Navigation auswählen</h1>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user