/
template.go
65 lines (48 loc) · 1.83 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
package core
// Template produces a string using a given context.
type Template interface {
// Styler used to format the templates content.
Styler() Styler
// Render generates this template using the given variable context.
Render(context interface{}) (string, error)
}
// TemplateFunc is an adapter to use a function as a Template.
type TemplateFunc func(context interface{}) (string, error)
// Styler implements Template.
func (f TemplateFunc) Styler() Styler {
return NullStyler
}
// Render implements Template.
func (f TemplateFunc) Render(context interface{}) (string, error) {
return f(context)
}
// NullTemplate is a Template always returning an empty string.
var NullTemplate = nullTemplate{}
type nullTemplate struct{}
func (t nullTemplate) Styler() Styler {
return NullStyler
}
func (t nullTemplate) Render(context interface{}) (string, error) {
return "", nil
}
// TemplateLoader parses a string into a new Template instance.
type TemplateLoader interface {
// LoadTemplate creates a Template instance from a string template.
LoadTemplate(template string) (Template, error)
// LoadTemplate creates a Template instance from a template stored in the
// file at the given path.
// The path may be relative to template directories registered to the loader.
LoadTemplateAt(path string) (Template, error)
}
// TemplateLoaderFactory creates a new instance of an implementation of the
// TemplateLoader port.
type TemplateLoaderFactory func(language string) (TemplateLoader, error)
// NullTemplateLoader a TemplateLoader always returning a NullTemplate.
var NullTemplateLoader = nullTemplateLoader{}
type nullTemplateLoader struct{}
func (t nullTemplateLoader) LoadTemplate(template string) (Template, error) {
return &NullTemplate, nil
}
func (t nullTemplateLoader) LoadTemplateAt(path string) (Template, error) {
return &NullTemplate, nil
}