-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
47 lines (38 loc) · 1.27 KB
/
container.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
package leaf
import (
"errors"
"html/template"
"io"
)
// ErrTemplateNotFound is returned when a template is not found.
var ErrTemplateNotFound = errors.New("template not found")
// Container describes an immutable interface for a structure holding a list of templates.
type Container interface {
// Get tries to fetch a template from the container.
//
// Returns an error when the template cannot be found or cannot be loaded in case of a dynamic container.
Get(name string) (*template.Template, error)
// Execute fetches and executes the template.
//
// Returns an error when the template cannot be found or cannot be loaded in case of a dynamic container.
// If the execution ends up in an error, it's returned as well.
Execute(name string, wr io.Writer, data interface{}) error
}
// container is the default, immutable implementation of Container returned by Builder.Compile.
type container struct {
templates map[string]*template.Template
}
func (c *container) Get(name string) (*template.Template, error) {
t, ok := c.templates[name]
if !ok {
return nil, ErrTemplateNotFound
}
return t, nil
}
func (c *container) Execute(name string, wr io.Writer, data interface{}) error {
tpl, err := c.Get(name)
if err != nil {
return err
}
return tpl.Execute(wr, data)
}