-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
103 lines (94 loc) · 2.51 KB
/
config.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
package render
import (
"path/filepath"
"strings"
"github.com/webx-top/echo"
"github.com/webx-top/echo/handler/mvc/static/resource"
"github.com/webx-top/echo/middleware"
"github.com/webx-top/echo/middleware/render/driver"
"github.com/webx-top/echo/middleware/tplfunc"
)
type Config struct {
TmplDir string
Theme string
Engine string
Style string
Reload bool
ParseStrings map[string]string
ErrorPages map[int]string
StaticOptions *middleware.StaticOptions
Debug bool
renderer driver.Driver
}
func (t *Config) Parser() func([]byte) []byte {
if t.ParseStrings == nil {
return nil
}
return func(b []byte) []byte {
s := string(b)
for oldVal, newVal := range t.ParseStrings {
s = strings.Replace(s, oldVal, newVal, -1)
}
return []byte(s)
}
}
// NewRenderer 新建渲染接口
func (t *Config) NewRenderer(manager ...driver.Manager) driver.Driver {
tmplDir := t.TmplDir
if len(t.Theme) > 0 {
tmplDir = filepath.Join(tmplDir, t.Theme)
}
renderer := New(t.Engine, tmplDir)
renderer.Init(true, t.Reload)
if len(manager) > 0 {
renderer.SetManager(manager[0])
}
renderer.SetContentProcessor(t.Parser())
if t.StaticOptions != nil {
st := t.NewStatic()
renderer.SetFuncMap(func() map[string]interface{} {
return st.Register(nil)
})
absTmplPath, err := filepath.Abs(tmplDir)
var absFilePath string
if err == nil {
absFilePath, err = filepath.Abs(t.StaticOptions.Root)
}
if err == nil {
if strings.HasPrefix(absFilePath, absTmplPath) {
//如果静态文件在模板的子文件夹时,监控模板时判断静态文件更改
renderer.MonitorEvent(st.OnUpdate(tmplDir))
}
}
}
return renderer
}
func (t *Config) ApplyTo(e *echo.Echo, manager ...driver.Manager) *Config {
if t.renderer != nil {
t.renderer.Close()
}
e.SetHTTPErrorHandler(HTTPErrorHandler(t.ErrorPages))
e.Use(middleware.FuncMap(tplfunc.New(), func(c echo.Context) bool {
return c.Format() != `html`
}))
renderer := t.NewRenderer(manager...)
if t.StaticOptions != nil {
e.Use(middleware.Static(t.StaticOptions))
}
e.SetRenderer(renderer)
t.renderer = renderer
return t
}
func (t *Config) Renderer() driver.Driver {
return t.renderer
}
func (t *Config) NewStatic() *resource.Static {
return resource.NewStatic(t.StaticOptions.Path, t.StaticOptions.Root)
}
// ThemeDir 主题所在文件夹的路径
func (t *Config) ThemeDir(args ...string) string {
if len(args) < 1 {
return filepath.Join(t.TmplDir, t.Theme)
}
return filepath.Join(t.TmplDir, args[0])
}