diff options
author | xengineering <me@xengineering.eu> | 2023-02-08 20:53:20 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-02-08 21:01:27 +0100 |
commit | f9a5140071703faf0c7515a05f52e69fdc1f11ba (patch) | |
tree | ee073f3c7fc1a6bbda1eee6e3bec076e73363740 /templates.go | |
parent | 9005de11ef8b7cf32cc8503e8b5f134eca47b4fb (diff) | |
download | ceres-f9a5140071703faf0c7515a05f52e69fdc1f11ba.tar ceres-f9a5140071703faf0c7515a05f52e69fdc1f11ba.tar.zst ceres-f9a5140071703faf0c7515a05f52e69fdc1f11ba.zip |
Move all sources to package main
This project is not so big that it needs multiple packages.
Diffstat (limited to 'templates.go')
-rw-r--r-- | templates.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..84e449a --- /dev/null +++ b/templates.go @@ -0,0 +1,32 @@ + +package main + +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 + } +} |