-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
56 lines (48 loc) · 1.24 KB
/
funcs.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 html
import (
"html/template"
"strings"
)
var (
defaultFuncs = map[string]interface{}{
// Skips sanitation on the parameter.
"raw": func(text string) template.HTML {
return template.HTML(text)
},
// Replaces newlines with '<br>'.
"nl2br": func(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
},
}
)
func createFuncMap(ldr *Loader) map[string]interface{} {
runView := func(view *View, data ...interface{}) (template.HTML, error) {
html, err := view.HTML(data...)
if err != nil {
return "", err
}
return template.HTML(html), nil
}
runSet := func(set *Set, data ...interface{}) (template.HTML, error) {
view, err := ldr.NewSet().AddSet(set).View()
if err != nil {
return "", err
}
return runView(view, data...)
}
runTemplate := func(name string, data ...interface{}) (template.HTML, error) {
view, err := ldr.NewSet().Add(name).View()
if err != nil {
return "", err
}
return runView(view, data...)
}
funcs := make(map[string]interface{}, len(defaultFuncs)+3)
funcs["runSet"] = runSet
funcs["runView"] = runView
funcs["runTemplate"] = runTemplate
for name, fn := range defaultFuncs {
funcs[name] = fn
}
return funcs
}