From 7d131bea17205c314387700787286365cbd98d5a Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sun, 11 Feb 2024 21:19:09 +0000 Subject: wip: Cannot get the dynamic data to show up in template --- cmd/web/handlers.go | 18 ++++++++++++------ cmd/web/templates.go | 7 +++++++ ui/html/pages/operations/list.tmpl.html | 12 +++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 cmd/web/templates.go diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 49f52ca..40ca509 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "html/template" + "log" "net/http" "strconv" @@ -12,12 +13,12 @@ import ( func (app *application) listOperations(w http.ResponseWriter, r *http.Request) { - // ops, err := app.operations.ListAll() + ops, err := app.operations.ListAll() - // if err != nil { - // app.serverError(w, r, err) - // return - // } + if err != nil { + app.serverError(w, r, err) + return + } files := []string{ "./ui/html/base.tmpl.html", "./ui/html/pages/operations/list.tmpl.html", @@ -30,7 +31,12 @@ func (app *application) listOperations(w http.ResponseWriter, r *http.Request) { return } - err = ts.ExecuteTemplate(w, "base", nil) + data := operationsTemplateData{ + Operations: ops, + } + log.Println(data) + + err = ts.ExecuteTemplate(w, "base", data) if err != nil { app.serverError(w, r, err) // Use the serverError() helper } diff --git a/cmd/web/templates.go b/cmd/web/templates.go new file mode 100644 index 0000000..86e136d --- /dev/null +++ b/cmd/web/templates.go @@ -0,0 +1,7 @@ +package main + +import "github.com/yulqen/ded-go-core/internal/models" + +type operationsTemplateData struct { + Operations []models.Operation +} diff --git a/ui/html/pages/operations/list.tmpl.html b/ui/html/pages/operations/list.tmpl.html index c377129..046ca29 100644 --- a/ui/html/pages/operations/list.tmpl.html +++ b/ui/html/pages/operations/list.tmpl.html @@ -2,7 +2,17 @@ {{ define "main" }}

Operations [Admin]

-

There is nothing to see here yet.

+ + {{if .Operations}} + + {{else}} +

There are no operations.

+ {{end}} + -- cgit v1.2.3