-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringfs.go
38 lines (31 loc) · 868 Bytes
/
stringfs.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
package template
import (
"io"
"io/ioutil"
"strings"
"github.com/CloudyKit/jet/v6"
)
type stringLoader struct {
templates map[string]string
}
// NewLoader returns an initialized loader serving the passed http.FileSystem.
func NewStringLoader() jet.Loader {
templates := make(map[string]string)
return &stringLoader{
templates: templates,
}
}
func (l *stringLoader) PutTemplateString(key string, template string) {
l.templates[key] = template
}
// Open implements Loader.Open() on top of an http.FileSystem.
func (l *stringLoader) Open(name string) (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader(l.templates[name])), nil
}
// Exists implements Loader.Exists() on top of an http.FileSystem by trying to open the file.
func (l *stringLoader) Exists(name string) bool {
if l.templates[name] != "" {
return true
}
return false
}