This commit is contained in:
lbenedar
2026-03-05 11:57:45 +03:00
parent c999dce1fd
commit bdbda690a2
3 changed files with 22 additions and 9 deletions

View File

@@ -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) {

View File

@@ -2,16 +2,25 @@ package main
import (
"html/template"
"net/http"
"path/filepath"
"time"
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
)
type templateData struct {
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) {
cache := map[string]*template.Template{}

View File

@@ -16,7 +16,9 @@
<main>
{{template "main" .}}
</main>
<footer>Powered by <a href='https://golang.org/'>Go</a></footer>
<footer>
Powered by <a href='https://golang.org/'>Go</a> in {{.CurrentYear}}
</footer>
<script src="/static/js/main.js" type="text/javascript"></script>
</body>
</html>