-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
52 lines (43 loc) · 1.13 KB
/
view.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
package carbonate
import (
"bytes"
)
// The View interface
type View interface {
ControllerChild
setPackageName(string)
}
// The struct to be inherited by other module specific views
// Contains the name of the package and the output buffer
type BaseView struct {
base string
output bytes.Buffer
}
// Set the parent controller
func (v *BaseView) setParent(c *BaseController) {
c.view = v
}
// Set the name of the module
func (v *BaseView) setPackageName(base string) {
v.base = base
checkAndParsePackageTemplates(base)
}
// Render a template and write it to the output buffer
func (v *BaseView) RenderTemplate(name string, data interface{}) {
html := v.RenderTemplateAsString(name, data)
v.output.WriteString(html)
}
// Render a template and return the results as string
func (v *BaseView) RenderTemplateAsString(name string, data interface{}) string {
template := getRelevantTemplate(name, v.base)
var b bytes.Buffer
// Check if the template is valid
if template != nil {
// If it is, then render it
template.Execute(&b, data)
} else {
// Otherwise, we have a problem
b.WriteString("No template found")
}
return b.String()
}