diff options
author | xengineering <me@xengineering.eu> | 2022-11-05 21:25:31 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2022-11-07 21:17:44 +0100 |
commit | 1d6b45bebea66391a2a535a3bb328a5732aaa75d (patch) | |
tree | 12faa62d8d8574ad8c94f4a7c9ff206c34456430 /utils/templates.go | |
download | ceres-1d6b45bebea66391a2a535a3bb328a5732aaa75d.tar ceres-1d6b45bebea66391a2a535a3bb328a5732aaa75d.tar.zst ceres-1d6b45bebea66391a2a535a3bb328a5732aaa75d.zip |
Add existing work
Diffstat (limited to 'utils/templates.go')
-rw-r--r-- | utils/templates.go | 32 |
1 files changed, 32 insertions, 0 deletions
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 + } +} |