forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
106 lines (96 loc) · 2.86 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package render
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"github.com/aymerick/raymond"
"github.com/pkg/errors"
)
type templateRenderer struct {
*Engine
contentType string
names []string
}
func (s templateRenderer) ContentType() string {
return s.contentType
}
func (s *templateRenderer) Render(w io.Writer, data Data) error {
var yield raymond.SafeString
var err error
for _, name := range s.names {
yield, err = s.execute(name, data)
if err != nil {
return errors.WithMessage(errors.WithStack(err), name)
}
data["yield"] = yield
}
_, err = w.Write([]byte(yield))
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (s *templateRenderer) execute(name string, data Data) (raymond.SafeString, error) {
source, err := s.source(name)
if err != nil {
return raymond.SafeString(fmt.Sprintf("<pre>%s: %s</pre>", name, err.Error())), err
}
source.RegisterHelper("partial", func(name string, options *raymond.Options) raymond.SafeString {
d := data
for k, v := range options.Hash() {
d[k] = v
defer delete(data, k)
}
p, err := s.partial(name, d)
if err != nil {
return raymond.SafeString(fmt.Sprintf("<pre>%s: %s</pre>", name, err.Error()))
}
return p
})
yield, err := source.Exec(data)
if err != nil {
return raymond.SafeString(fmt.Sprintf("<pre>%s: %s</pre>", name, err.Error())), err
}
return raymond.SafeString(yield), nil
}
func (s *templateRenderer) source(name string) (*raymond.Template, error) {
b, err := ioutil.ReadFile(filepath.Join(s.TemplatesPath, name))
if err != nil {
return nil, errors.WithStack(fmt.Errorf("could not find template: %s", name))
}
t, err := raymond.Parse(string(b))
if err != nil {
return t, errors.WithMessage(errors.WithStack(err), name)
}
t.RegisterHelpers(s.Helpers)
return t, err
}
func (s *templateRenderer) partial(name string, data Data) (raymond.SafeString, error) {
d, f := filepath.Split(name)
name = filepath.Join(d, "_"+f)
return s.execute(name, data)
}
// Template renders the named files using the specified
// content type and the github.com/aymerick/raymond
// package for templating. If more than 1 file is provided
// the second file will be considered a "layout" file
// and the first file will be the "content" file which will
// be placed into the "layout" using "{{yield}}".
func Template(c string, names ...string) Renderer {
e := New(Options{})
return e.Template(c, names...)
}
// Template renders the named files using the specified
// content type and the github.com/aymerick/raymond
// package for templating. If more than 1 file is provided
// the second file will be considered a "layout" file
// and the first file will be the "content" file which will
// be placed into the "layout" using "{{yield}}".
func (e *Engine) Template(c string, names ...string) Renderer {
return &templateRenderer{
Engine: e,
contentType: c,
names: names,
}
}