From bdbda690a20320238e20eab324570a28f9e165e6 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Thu, 5 Mar 2026 11:57:45 +0300 Subject: [PATCH] ch5.5 --- cmd/web/handlers.go | 14 ++++++++------ cmd/web/templates.go | 13 +++++++++++-- ui/html/base.tmpl | 4 +++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 83b74e9..2976535 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -39,9 +39,10 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { app.serverError(w, err) return } - app.render(w, http.StatusOK, "home.tmpl", &templateData{ - Snippets: snippets, - }) + data := app.newTemplateData(r) + data.Snippets = snippets + + app.render(w, http.StatusOK, "home.tmpl", data) } func (app *application) snippetView(w http.ResponseWriter, r *http.Request) { @@ -60,9 +61,10 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) { } return } - app.render(w, http.StatusOK, "view.tmpl", &templateData{ - Snippet: snippet, - }) + data := app.newTemplateData(r) + data.Snippet = snippet + + app.render(w, http.StatusOK, "view.tmpl", data) } func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/web/templates.go b/cmd/web/templates.go index 7c858c1..90996c1 100644 --- a/cmd/web/templates.go +++ b/cmd/web/templates.go @@ -2,14 +2,23 @@ package main import ( "html/template" + "net/http" "path/filepath" + "time" "gitea.local.lab/Lbenedar/snippetbox/internal/models" ) type templateData struct { - Snippet *models.Snippet - Snippets []*models.Snippet + CurrentYear int + Snippet *models.Snippet + Snippets []*models.Snippet +} + +func (app *application) newTemplateData(r *http.Request) *templateData { + return &templateData{ + CurrentYear: time.Now().Year(), + } } func newTemplateCache() (map[string]*template.Template, error) { diff --git a/ui/html/base.tmpl b/ui/html/base.tmpl index 00ee1a1..8344b94 100644 --- a/ui/html/base.tmpl +++ b/ui/html/base.tmpl @@ -16,7 +16,9 @@
{{template "main" .}}
- +