From 0db8a9f74df83c090b43e70701a600644ee33f1f Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Thu, 25 Jan 2024 14:08:58 -0800 Subject: [PATCH] lets-go:5.2 home view --- snippetbox/cmd/web/handlers.go | 32 +++++++++++++++--------------- snippetbox/cmd/web/templates.go | 1 + snippetbox/ui/html/pages/home.tmpl | 19 +++++++++++++++++- snippetbox/ui/html/pages/view.tmpl | 12 ++++++----- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index bac7b33..94dcf53 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -23,26 +23,26 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { return } - for _, snippet := range snippets { - fmt.Fprintf(w, "%+v", snippet) + files := []string{ + "./ui/html/base.tmpl", + "./ui/html/partials/nav.tmpl", + "./ui/html/pages/home.tmpl", } - // files := []string{ - // "./ui/html/base.tmpl", - // "./ui/html/partials/nav.tmpl", - // "./ui/html/pages/home.tmpl", - // } + ts, err := template.ParseFiles(files...) + if err != nil { + app.serverError(w, r, err) + return + } - // ts, err := template.ParseFiles(files...) - // if err != nil { - // app.serverError(w, r, err) - // return - // } + data := templateData{ + Snippets: snippets, + } - // err = ts.ExecuteTemplate(w, "base", nil) - // if err != nil { - // app.serverError(w, r, err) - // } + err = ts.ExecuteTemplate(w, "base", data) + if err != nil { + app.serverError(w, r, err) + } } // snippetView ... diff --git a/snippetbox/cmd/web/templates.go b/snippetbox/cmd/web/templates.go index fa0f998..61e5975 100644 --- a/snippetbox/cmd/web/templates.go +++ b/snippetbox/cmd/web/templates.go @@ -6,4 +6,5 @@ import ( type templateData struct { Snippet models.Snippet + Snippets []models.Snippet } diff --git a/snippetbox/ui/html/pages/home.tmpl b/snippetbox/ui/html/pages/home.tmpl index 534188a..53dc621 100644 --- a/snippetbox/ui/html/pages/home.tmpl +++ b/snippetbox/ui/html/pages/home.tmpl @@ -2,5 +2,22 @@ {{define "main"}}

Latest Snippets

-

leave me alone!

+ {{if .Snippets}} + + + + + + + {{range .Snippets}} + + + + + + {{else}} +

There's nothing to see here... yet!

+ {{end}} +
TitleCreatedID
{{.Title}}{{.Created}}{{.ID}}
+ {{end}} {{end}} diff --git a/snippetbox/ui/html/pages/view.tmpl b/snippetbox/ui/html/pages/view.tmpl index 0cd8f92..f2111aa 100644 --- a/snippetbox/ui/html/pages/view.tmpl +++ b/snippetbox/ui/html/pages/view.tmpl @@ -1,16 +1,18 @@ {{define "title"}}Snippet #{{.Snippet.ID}}{{end}} {{define "main"}} + {{with .Snippet}}
-
{{.Snippet.Content}}
+
{{.Content}}
+ {{end}} {{end}}