From b526edbec43d4a181a22a64bc34d84b0c71f861b Mon Sep 17 00:00:00 2001 From: Markus Zimmermann Date: Sat, 15 Feb 2025 08:17:25 +0100 Subject: [PATCH] Introduce `templateutil.Template` as interface to `html/template.Template` and `text/template.Template` --- templateutil/file.go | 2 +- templateutil/interface.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 templateutil/interface.go diff --git a/templateutil/file.go b/templateutil/file.go index c9af39e..b7fb761 100644 --- a/templateutil/file.go +++ b/templateutil/file.go @@ -12,7 +12,7 @@ import ( ) // WriteTemplateToFile executes a template with the given data and saves the result into a file. -func WriteTemplateToFile(filePath string, tmpl *template.Template, data any) error { +func WriteTemplateToFile(filePath string, tmpl Template, data any) error { var driver bytes.Buffer err := tmpl.Execute(&driver, data) diff --git a/templateutil/interface.go b/templateutil/interface.go new file mode 100644 index 0000000..1dc4f12 --- /dev/null +++ b/templateutil/interface.go @@ -0,0 +1,8 @@ +package templateutil + +import "io" + +type Template interface { + // Execute applies the template to the specified data object and writes the output to the writer. + Execute(wr io.Writer, data any) (err error) +}