diff --git a/cmd/web/templates.go b/cmd/web/templates.go index 90996c1..768a643 100644 --- a/cmd/web/templates.go +++ b/cmd/web/templates.go @@ -15,6 +15,14 @@ type templateData struct { Snippets []*models.Snippet } +func humanDate(t time.Time) string { + return t.Format("02 Jan 2006 at 15:04") +} + +var functions = template.FuncMap{ + "humanDate": humanDate, +} + func (app *application) newTemplateData(r *http.Request) *templateData { return &templateData{ CurrentYear: time.Now().Year(), @@ -31,7 +39,7 @@ func newTemplateCache() (map[string]*template.Template, error) { for _, page := range pages { name := filepath.Base(page) - ts, err := template.ParseFiles("./ui/html/base.tmpl") + ts, err := template.New(name).Funcs(functions).ParseFiles("./ui/html/base.tmpl") if err != nil { return nil, err } diff --git a/ui/html/pages/home.tmpl b/ui/html/pages/home.tmpl index ea0b4ea..fa01fbf 100644 --- a/ui/html/pages/home.tmpl +++ b/ui/html/pages/home.tmpl @@ -12,7 +12,7 @@ {{range .Snippets}}
{{.Content}}