forked from hashicorp/otto
/
tpl_loader.go
49 lines (40 loc) · 928 Bytes
/
tpl_loader.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
package bindata
import (
"bytes"
"fmt"
"io"
"path/filepath"
"strings"
)
// tplLoader is a pongo2.TemplateLoader that loads templates from assets.
type tplLoader struct {
Data *Data
Base string
}
func (t *tplLoader) Abs(base, name string) string {
idx := strings.IndexAny(name, ":/")
if idx != -1 && name[idx] == ':' {
// We have a shared resource. Return the name as-is
return name
}
return filepath.Join(t.Base, name)
}
func (t *tplLoader) Get(path string) (io.Reader, error) {
data := t.Data
idx := strings.IndexAny(path, ":/")
if idx != -1 && path[idx] == ':' {
// We have a shared resource. Get the proper data loader.
share := path[:idx]
raw, ok := t.Data.SharedExtends[share]
if !ok {
return nil, fmt.Errorf("share not found: %s", share)
}
data = raw
path = path[idx+1:]
}
raw, err := data.Asset(path)
if err != nil {
return nil, err
}
return bytes.NewReader(raw), nil
}