/
context.go
141 lines (122 loc) · 3.37 KB
/
context.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package admin
import (
"bytes"
"errors"
"fmt"
"html/template"
"os"
"path"
"strings"
"github.com/qor/qor"
)
type Context struct {
*qor.Context
*Searcher
Flashs []Flash
Resource *Resource
Admin *Admin
Result interface{}
Content template.HTML
}
// Resource
func (context *Context) resourcePath() string {
if context.Resource == nil {
return ""
}
return context.Resource.ToParam()
}
func (context *Context) setResource(res *Resource) *Context {
context.Resource = res
context.Searcher = &Searcher{Context: context}
return context
}
func (context *Context) GetResource(name string) *Resource {
return context.Admin.GetResource(name)
}
// Template
func (context *Context) getViewPaths() (paths []string) {
var dirs = []string{context.resourcePath(), path.Join("themes", "default"), "."}
var themes []string
if context.Request != nil {
if theme := context.Request.URL.Query().Get("theme"); theme != "" {
themePath := path.Join("themes", theme)
themes = append(themes, []string{path.Join(themePath, context.resourcePath()), themePath}...)
}
}
if context.Resource != nil {
for _, theme := range context.Resource.Config.Themes {
themePath := path.Join("themes", theme)
themes = append(themes, []string{path.Join(themePath, context.resourcePath()), themePath}...)
}
}
for _, p := range append(themes, dirs...) {
for _, d := range viewPaths {
if isExistingDir(path.Join(d, p)) {
paths = append(paths, path.Join(d, p))
}
}
}
return paths
}
func (context *Context) findFile(layout string) (string, error) {
for _, p := range context.getViewPaths() {
if _, err := os.Stat(path.Join(p, layout)); !os.IsNotExist(err) {
return path.Join(p, layout), nil
}
}
return "", errors.New("file not found")
}
func (context *Context) FindTemplate(tmpl *template.Template, layout string) (*template.Template, error) {
for _, p := range context.getViewPaths() {
if _, err := os.Stat(path.Join(p, layout)); !os.IsNotExist(err) {
if tmpl, err = tmpl.ParseFiles(path.Join(p, layout)); err != nil {
fmt.Println(err)
} else {
return tmpl, nil
}
}
}
return tmpl, errors.New("template not found")
}
func (context *Context) Render(name string, results ...interface{}) template.HTML {
var err error
names := strings.Split(name, "/")
tmpl := template.New(names[len(names)-1] + ".tmpl").Funcs(context.FuncMap())
if len(results) > 0 {
context.Result = results[0]
}
if tmpl, err = context.FindTemplate(tmpl, name+".tmpl"); err == nil {
var result = bytes.NewBufferString("")
if err := tmpl.Execute(result, context); err != nil {
fmt.Println(err)
}
return template.HTML(result.String())
}
return ""
}
func (context *Context) Execute(name string, result interface{}) {
var tmpl *template.Template
var cacheKey string
if context.Resource != nil {
cacheKey = path.Join(context.resourcePath(), name)
} else {
cacheKey = name
}
if t, ok := templates[cacheKey]; !ok || true {
var err error
tmpl = template.New("layout.tmpl").Funcs(context.FuncMap())
if tmpl, err = context.FindTemplate(tmpl, "layout.tmpl"); err == nil {
for _, name := range []string{"header", "footer"} {
if tmpl.Lookup(name) == nil {
tmpl, _ = context.FindTemplate(tmpl, name+".tmpl")
}
}
}
} else {
tmpl = t
}
context.Content = context.Render(name, result)
if err := tmpl.Execute(context.Writer, context); err != nil {
fmt.Println(err)
}
}