-
Notifications
You must be signed in to change notification settings - Fork 3
/
renderer.go
32 lines (27 loc) · 901 Bytes
/
renderer.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
package tmpl
import (
"fmt"
)
// Renderer is interface for template rendering engine adapters. Engines process data (map[string]interface{} or
// []interfaces{}) using provided template into combined result.
//
type Renderer interface {
Render(data interface{}, template []byte) ([]byte, error)
}
var Renderers = map[string]func() Renderer{}
var RendererGuesses = []func(location string) Renderer{}
// GuessRenderer tries to determine
func GuessRenderer(location string) (Renderer, error) {
for _, builder := range RendererGuesses {
if renderer := builder(location); renderer != nil {
return renderer, nil
}
}
return nil, fmt.Errorf("could not guess renderer for location %s", location)
}
func BuildRenderer(name string) (Renderer, error) {
if builder, ok := Renderers[name]; ok {
return builder(), nil
}
return nil, fmt.Errorf("no rendered with name \"%s\" registered", name)
}