From c05b34f4cdea5f0c62fcef325f37a32d75da3685 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sat, 21 Sep 2024 13:09:43 -0400 Subject: [PATCH] feat: add render duration in footer --- static/main.css | 15 ++++++++++++--- templates.go | 25 +++++++++++++++++++++++-- templates/base.html.tmpl | 1 + templates/footer.html.tmpl | 7 +++++++ templates/index.html.tmpl | 2 +- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 templates/footer.html.tmpl diff --git a/static/main.css b/static/main.css index 66b2b68..8888c6d 100644 --- a/static/main.css +++ b/static/main.css @@ -2,16 +2,16 @@ body { margin: 0; } -header { +body > header { padding: 10px; } -header > h1 { +body > header > h1 { margin: 0; font-size: 1.2em; } -header ul { +body > header ul { list-style: none; display: flex; gap: 5px; @@ -19,6 +19,15 @@ header ul { padding: 5px; } +body > footer { + font-size: 0.9em; + margin-top: 10px; + padding: 10px; + display: flex; + gap: 5px; + justify-content: center; +} + #items { list-style: none; padding-left: 0; diff --git a/templates.go b/templates.go index cd9d894..6575506 100644 --- a/templates.go +++ b/templates.go @@ -5,8 +5,13 @@ import ( "io" "path/filepath" "text/template" + "time" ) +func getRenderDuration(start time.Time) time.Duration { + return time.Now().Sub(start) +} + func Render(w io.Writer, templateName string, data interface{}) error { asTemplatePath := func(templateName string) string { return filepath.Join("templates", fmt.Sprintf("%s.html.tmpl", templateName)) @@ -15,13 +20,29 @@ func Render(w io.Writer, templateName string, data interface{}) error { templatePartials := []string{ asTemplatePath(templateName), asTemplatePath("base"), + asTemplatePath("footer"), } - tmpl, err := template.New(templateName).ParseFiles(templatePartials...) + start := time.Now() + + tmpl := template.New(templateName).Funcs(template.FuncMap{ + "getRenderDuration": getRenderDuration, + }) + + tmpl, err := tmpl.ParseFiles(templatePartials...) if err != nil { return err } - return tmpl.ExecuteTemplate(w, "base", data) + type RenderMeta struct { + RenderStart time.Time + } + + type RenderContext struct { + Data interface{} + Meta RenderMeta + } + + return tmpl.ExecuteTemplate(w, "base", RenderContext{Data: data, Meta: RenderMeta{RenderStart: start}}) } diff --git a/templates/base.html.tmpl b/templates/base.html.tmpl index 5968ab9..7127415 100644 --- a/templates/base.html.tmpl +++ b/templates/base.html.tmpl @@ -17,6 +17,7 @@ {{ template "content" . }} + {{ template "footer" . }} {{ end }} diff --git a/templates/footer.html.tmpl b/templates/footer.html.tmpl new file mode 100644 index 0000000..b683307 --- /dev/null +++ b/templates/footer.html.tmpl @@ -0,0 +1,7 @@ +{{ define "footer" }} + +{{ end }} diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index 56fbd2c..4b98f3d 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -1,6 +1,6 @@ {{ define "content" }}