-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_view.go
56 lines (45 loc) · 1.22 KB
/
context_view.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
package kira
import (
"bytes"
"os"
"path/filepath"
)
// View send an html/template with an HTTP reply.
func (c *Context) View(temps string, data ...interface{}) error {
// Set content type
c.Response().Header().Set("Content-Type", "text/html")
// parse the templates
template, err := parseView(c, temps, data)
if err != nil {
return err
}
// execute the templates
err = template.Execute(c.Response(), parseViewData(data...))
if err != nil {
return err
}
return nil
}
// ViewToString parse a template and return the parsed template as a string.
func (c *Context) ViewToString(temps string, data ...interface{}) (string, error) {
template, err := parseView(c, temps, data)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
err = template.Execute(buf, parseViewData(data...))
if err != nil {
return "", err
}
return buf.String(), nil
}
// ViewExists Validate if the view exists.
func (c *Context) ViewExists(tmp string) bool {
fileSuffix := c.Config().GetString("views.file_suffix", ".go.html")
viewPath := c.Config().GetString("views.path", "./app/views/")
templatePath := filepath.Join(viewPath, tmp+fileSuffix)
if _, err := os.Stat(templatePath); os.IsNotExist(err) {
return false
}
return true
}