From b3b56754c244f6a6abd2ce786831e5d95e579a5e Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sat, 21 Sep 2024 13:16:15 -0400 Subject: [PATCH] refactor: template renderer cleanup --- templates.go => render_templates.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) rename templates.go => render_templates.go (54%) diff --git a/templates.go b/render_templates.go similarity index 54% rename from templates.go rename to render_templates.go index 6575506..9bbf135 100644 --- a/templates.go +++ b/render_templates.go @@ -1,3 +1,10 @@ +// Template rendering +// +// Abstracts the rendering of static HTML templates. +// Render(...) handles rendering the provided template with the +// default partial templates (header, footer, general base...), handles +// getting the paths to those template files and feeding the data (including +// some internally-generated context) when hydrating the template. package main import ( @@ -8,6 +15,15 @@ import ( "time" ) +type RenderMeta struct { + RenderStart time.Time +} + +type RenderContext struct { + Data interface{} + Meta RenderMeta +} + func getRenderDuration(start time.Time) time.Duration { return time.Now().Sub(start) } @@ -35,14 +51,7 @@ func Render(w io.Writer, templateName string, data interface{}) error { return err } - type RenderMeta struct { - RenderStart time.Time - } + renderContext := RenderContext{Data: data, Meta: RenderMeta{RenderStart: start}} - type RenderContext struct { - Data interface{} - Meta RenderMeta - } - - return tmpl.ExecuteTemplate(w, "base", RenderContext{Data: data, Meta: RenderMeta{RenderStart: start}}) + return tmpl.ExecuteTemplate(w, "base", renderContext) }