-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvelvet.go
52 lines (43 loc) · 1.03 KB
/
velvet.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
package velvet
import (
"sync"
"github.com/pkg/errors"
)
// BuffaloRenderer implements the render.TemplateEngine interface allowing velvet to be used as a template engine
// for Buffalo
func BuffaloRenderer(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error) {
t, err := Parse(input)
if err != nil {
return "", err
}
if helpers != nil {
t.Helpers.AddMany(helpers)
}
return t.Exec(NewContextWith(data))
}
var cache = map[string]*Template{}
var moot = &sync.Mutex{}
// Parse an input string and return a Template.
func Parse(input string) (*Template, error) {
moot.Lock()
defer moot.Unlock()
if t, ok := cache[input]; ok {
return t, nil
}
t, err := NewTemplate(input)
if err == nil {
cache[input] = t
}
if err != nil {
return t, errors.WithStack(err)
}
return t, nil
}
// Render a string using the given the context.
func Render(input string, ctx *Context) (string, error) {
t, err := Parse(input)
if err != nil {
return "", errors.WithStack(err)
}
return t.Exec(ctx)
}