This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.go
163 lines (135 loc) · 3.86 KB
/
template.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package kwiscale
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"path/filepath"
"reflect"
"regexp"
"strings"
)
var templateEngine = make(map[string]reflect.Type)
// TplOptions are template options to pass to template engines if needed
type TplOptions map[string]interface{}
// RegisterTemplateEngine records template engine that implements Template
// interface. The name is used to let config select the template engine.
func RegisterTemplateEngine(name string, tpl Template) {
templateEngine[name] = reflect.ValueOf(tpl).Elem().Type()
}
// register basic template engine by default.
func init() {
RegisterTemplateEngine("basic", &BuiltInTemplate{})
}
// Template should be implemented by other template implementation to
// allow RequestHandlers to use Render() method
type Template interface {
// Render method to implement to compile and run template
// then write to RequestHandler "w" that is a io.Writer.
Render(w io.Writer, template string, ctx interface{}) error
// SetTemplateDir should set the template base directory
SetTemplateDir(string)
// SetOptions pass TplOptions to template engine
SetTemplateOptions(TplOptions)
}
// BuiltInTemplate is Basic template engine that use html/template.
type BuiltInTemplate struct {
files []string
tpldir string
funcMap template.FuncMap
}
// SetTemplateDir set the directory where are found templates.
func (tpl *BuiltInTemplate) SetTemplateDir(path string) {
t, err := filepath.Abs(path)
if err != nil {
panic(err)
}
tpl.tpldir = t
Log("Template dir set to ", tpl.tpldir)
}
// Render method for the basic Template system.
// Allow {{/* override "path.html" */}}, no cache, very basic.
func (tpl *BuiltInTemplate) Render(w io.Writer, file string, ctx interface{}) error {
var err error
defer func() {
if err != nil {
Error(err)
}
}()
file = filepath.Join(tpl.tpldir, file)
tpl.files = make([]string, 0)
content, err := ioutil.ReadFile(file)
// panic if read file breaks
if err != nil {
panic(err)
}
tpl.parseOverride(content)
tpl.files = append(tpl.files, file)
Log(tpl.files)
if tpl.funcMap == nil {
tpl.funcMap = template.FuncMap{}
}
tpl.funcMap["static"] = func(file string) string {
app := w.(WebHandler).App()
url, err := app.GetRoute("statics").URL("file", file)
if err != nil {
return err.Error()
}
return url.String()
}
tpl.funcMap["url"] = func(handler string, args ...interface{}) string {
pairs := []string{}
for _, p := range args {
pairs = append(pairs, fmt.Sprintf("%v", p))
}
h := w.(WebHandler).App().GetRoutes(handler)
base := []string{}
for _, r := range h {
url, err := r.URL(pairs...)
if err != nil {
continue
}
route := strings.Split(url.String(), "/")
if len(route) >= len(base) {
base = route
}
}
if len(base) == 0 {
return "handler url not realized - please check"
}
return strings.Join(base, "/")
}
t, err := template.
New(filepath.Base(tpl.files[0])).
Funcs(tpl.funcMap).
ParseFiles(tpl.files...)
// panic if template breaks in parse
if err != nil {
panic(err)
}
err = t.Execute(w, ctx)
// return error there !
return err
}
// SetTemplateOptions set needed options to template engine. For BuiltInTemplate
// there are no option at this time.
func (tpl *BuiltInTemplate) SetTemplateOptions(opts TplOptions) {
if m, ok := opts["funcs"]; ok {
for name, fn := range m.(template.FuncMap) {
tpl.funcMap[name] = fn
}
}
}
// parseOverride will append overriden templates to be integrating in the
// template list to render
func (tpl *BuiltInTemplate) parseOverride(content []byte) {
re := regexp.MustCompile(`\{\{/\*\s*override\s*\"?(.*?)\"?\s*\*/\}\}`)
matches := re.FindAllSubmatch(content, -1)
for _, m := range matches {
// find bottom templates
tplfile := filepath.Join(tpl.tpldir, string(m[1]))
c, _ := ioutil.ReadFile(tplfile)
tpl.parseOverride(c)
tpl.files = append(tpl.files, tplfile)
}
}