summaryrefslogtreecommitdiff
path: root/templates.go
blob: d18017ff51e92e1e7d82e50ae3e717c53764da84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"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
	}
}