ch5.5
This commit is contained in:
@@ -39,9 +39,10 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.serverError(w, err)
|
app.serverError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.render(w, http.StatusOK, "home.tmpl", &templateData{
|
data := app.newTemplateData(r)
|
||||||
Snippets: snippets,
|
data.Snippets = snippets
|
||||||
})
|
|
||||||
|
app.render(w, http.StatusOK, "home.tmpl", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
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
|
return
|
||||||
}
|
}
|
||||||
app.render(w, http.StatusOK, "view.tmpl", &templateData{
|
data := app.newTemplateData(r)
|
||||||
Snippet: snippet,
|
data.Snippet = snippet
|
||||||
})
|
|
||||||
|
app.render(w, http.StatusOK, "view.tmpl", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -2,14 +2,23 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type templateData struct {
|
type templateData struct {
|
||||||
Snippet *models.Snippet
|
CurrentYear int
|
||||||
Snippets []*models.Snippet
|
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) {
|
func newTemplateCache() (map[string]*template.Template, error) {
|
||||||
|
|||||||
@@ -16,7 +16,9 @@
|
|||||||
<main>
|
<main>
|
||||||
{{template "main" .}}
|
{{template "main" .}}
|
||||||
</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>
|
<script src="/static/js/main.js" type="text/javascript"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user