-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
106 lines (85 loc) · 2.36 KB
/
functions.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
/*
* Bulldozer Framework
* Copyright (C) DesertBit
*/
package template
import (
tr "github.com/desertbit/bulldozer/translate"
ht "html/template"
"bytes"
"fmt"
)
var (
bulldozerFuncMap FuncMap = FuncMap{
"tr": tr.S,
"plugin": renderPlugin,
"callFunc": callTemplateFunc,
"eventKeyVar": createEventAccessKeyFromVar,
"eventKey": createEventAccessKey,
"tmplR": renderTemplate,
"loadJS": loadJavaScript,
"loadStyle": loadStyleSheet,
}
)
//##################################//
//### Private template functions ###//
//##################################//
func renderTemplate(templateName string, id string, r *renderData, values ...interface{}) (ht.HTML, error) {
// Get the context.
c := r.Context
// Get the right sub template.
t := c.t.Lookup(templateName)
if t == nil {
return ht.HTML(""), fmt.Errorf("no template found with name: '%s'", templateName)
}
// Create the unique sub template ID if present.
// Otherwise use the template name as ID.
if len(id) > 0 {
id = c.data.ID + "_" + id
} else {
id = c.data.ID + "^" + templateName
}
// Create the new sub template context.
c = c.New(t, id)
// Create a new render data interface.
var data interface{}
// Get the length of the values
valuesLen := len(values)
// If only one value is set, then set it as the root data value.
if valuesLen == 1 {
data = values[0]
} else {
// Values have to be passed with keys.
if valuesLen%2 != 0 {
return ht.HTML(""), fmt.Errorf("invalid template call: values must have a key")
}
// Create a new values map
valuesMap := make(map[string]interface{}, valuesLen/2)
for i := 0; i < valuesLen; i += 2 {
key, ok := values[i].(string)
if !ok {
return ht.HTML(""), fmt.Errorf("invalid template call: keys must be of type string")
}
valuesMap[key] = values[i+1]
}
// Set the map as data interface
data = valuesMap
}
// Execute the sub template context.
var b bytes.Buffer
err := ExecuteContext(c, &b, data)
if err != nil {
return ht.HTML(""), fmt.Errorf("failed to render sub template '%s': %v", templateName, err)
}
return ht.HTML(b.String()), nil
}
func loadJavaScript(c *Context, url string) string {
// Load the javascript
c.ns.s.LoadJavaScript(url)
return ""
}
func loadStyleSheet(c *Context, url string) string {
// Load the javascript
c.ns.s.LoadStyleSheet(url)
return ""
}