-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateProvider.go
48 lines (36 loc) · 1022 Bytes
/
templateProvider.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
package diago
import (
"html/template"
"strings"
)
type TemplateProvider interface {
GetTemplate() string
}
type PanelGenerator interface {
GenerateHTML(name string, templateProvider TemplateProvider, data interface{}) (string, error)
}
type DefaultPanelGenerator struct{}
type DefaultTemplateProvider struct{}
func (p DefaultTemplateProvider) GetTemplate() string {
return GetDiagoPanelTemplate()
}
func (d *DefaultPanelGenerator) GenerateHTML(name string, templateProvider TemplateProvider, data interface{}) (string, error) {
templateContent := templateProvider.GetTemplate()
tp := template.New(name)
tpl, err := tp.Parse(templateContent)
if err != nil {
return "", err
}
var builder strings.Builder
err = tpl.Execute(&builder, data)
if err != nil {
return "", err
}
return builder.String(), nil
}
func NewDefaultPanelGenerator() *DefaultPanelGenerator {
return &DefaultPanelGenerator{}
}
func NewDefaultTemplateProvider() *DefaultTemplateProvider {
return &DefaultTemplateProvider{}
}