-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
36 lines (28 loc) · 801 Bytes
/
helper.go
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
32
33
34
package main
import(
"bytes"
"html/template"
"net/http"
"runtime"
"path/filepath"
"log"
)
func concat(params ...string) string{
var buffer bytes.Buffer
for _,str := range params{
buffer.WriteString(str);
}
return buffer.String();
}
func view(templateName string, data interface{},res http.ResponseWriter){
_,cwd,_,_ := runtime.Caller(0)
full_path:= filepath.Join(filepath.Dir(cwd),concat("templates/",templateName,`.html`))
t,err:= template.ParseFiles(full_path)
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(res, data) //execute the template and pass it the HomePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}