-
Notifications
You must be signed in to change notification settings - Fork 343
/
template.go
53 lines (42 loc) · 1.34 KB
/
template.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
53
// Package template provides a simple templating solution reusable in filters.
//
// (Note that the current template syntax is EXPERIMENTAL, and may change in
// the near future.)
package eskip
import (
"regexp"
"strings"
)
var parameterRegexp = regexp.MustCompile("\\$\\{(\\w+)\\}")
// TemplateGetter functions return the value for a template parameter name.
type TemplateGetter func(string) string
// Template represents a string template with named placeholders.
type Template struct {
template string
placeholders []string
}
// New parses a template string and returns a reusable *Template object.
// The template string can contain named placeholders of the format:
//
// Hello, ${who}!
//
func NewTemplate(template string) *Template {
matches := parameterRegexp.FindAllStringSubmatch(template, -1)
placeholders := make([]string, len(matches))
for index, placeholder := range matches {
placeholders[index] = placeholder[1]
}
return &Template{template: template, placeholders: placeholders}
}
// Apply evaluates the template using a TemplateGetter function to resolve the
// placeholders.
func (t *Template) Apply(get TemplateGetter) string {
result := t.template
if get == nil {
return result
}
for _, placeholder := range t.placeholders {
result = strings.Replace(result, "${"+placeholder+"}", get(placeholder), -1)
}
return result
}