From 1d6b45bebea66391a2a535a3bb328a5732aaa75d Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 5 Nov 2022 21:25:31 +0100 Subject: Add existing work --- utils/templates.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 utils/templates.go (limited to 'utils/templates.go') diff --git a/utils/templates.go b/utils/templates.go new file mode 100644 index 0000000..00a8eb2 --- /dev/null +++ b/utils/templates.go @@ -0,0 +1,32 @@ + +package utils + +import ( + "log" + "net/http" + "io/ioutil" + "text/template" // FIXME switch to html/template for security reasons + // and make a workaround for rendered Markdown insertion +) + +func ServeTemplate(w http.ResponseWriter, name string, path string, data interface{}) { + + templateFile,err := ioutil.ReadFile(path) + if err != nil { + log.Print(err) + http.Error(w, http.StatusText(404), 404) + return + } + tmpl,err := template.New(name).Parse(string(templateFile)) + if err != nil { + log.Print(err) + http.Error(w, http.StatusText(404), 404) + return + } + err = tmpl.Execute(w, data) + if err != nil { + log.Print(err) + http.Error(w, http.StatusText(404), 404) + return + } +} -- cgit v1.2.3-70-g09d2