-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
render.go
executable file
·83 lines (67 loc) · 1.68 KB
/
render.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// File generated by Gopher Sauce
// DO NOT EDIT!!
package templates
import (
"bytes"
"fmt"
"html"
"html/template"
"log"
"net/http"
gosweb "github.com/cheikhshift/gos/web"
"github.com/fatih/color"
)
// Renders an HTML response with variables provided.
func RenderTemplate(w http.ResponseWriter, p *gosweb.Page) {
defer func() {
if n := recover(); n != nil {
color.Red(fmt.Sprintf("Error loading template in path : web%s.tmpl reason : %s", p.R.URL.Path, n))
DebugTemplate(w, p.R, fmt.Sprintf("web%s", p.R.URL.Path))
w.WriteHeader(http.StatusInternalServerError)
pag, err := LoadPage("")
if err != nil {
log.Println(err.Error())
return
}
if pag.IsResource {
w.Write(pag.Body)
} else {
pag.R = p.R
pag.Session = p.Session
RenderTemplate(w, pag) //"
}
}
}()
if _, ok := templateCache.Get(p.R.URL.Path); !ok || !Prod {
var tmpstr = string(p.Body)
var localtemplate = template.New(p.R.URL.Path)
localtemplate.Funcs(TemplateFuncStore)
localtemplate.Parse(tmpstr)
templateCache.Put(p.R.URL.Path, localtemplate)
}
outp := new(bytes.Buffer)
err := templateCache.JGet(p.R.URL.Path).Execute(outp, p)
if err != nil {
log.Println(err.Error())
DebugTemplate(w, p.R, fmt.Sprintf("web%s", p.R.URL.Path))
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/html")
pag, err := LoadPage("")
if err != nil {
log.Println(err.Error())
return
}
pag.R = p.R
pag.Session = p.Session
if pag.IsResource {
w.Write(pag.Body)
} else {
RenderTemplate(w, pag)
}
return
}
var outps = outp.String()
var outpescaped = html.UnescapeString(outps)
outp = nil
fmt.Fprintf(w, outpescaped)
}