forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
125 lines (115 loc) · 3.38 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package render
import (
"bytes"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/aymerick/raymond"
"github.com/pkg/errors"
"github.com/shurcooL/github_flavored_markdown"
)
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) {
var t *raymond.Template
var ok bool
var err error
if s.CacheTemplates {
if t, ok = s.templateCache[name]; ok {
return t.Clone(), nil
}
}
b, err := s.FileResolver.Read(filepath.Join(s.TemplatesPath, name))
if err != nil {
return nil, errors.WithStack(fmt.Errorf("could not find template: %s", name))
}
if strings.ToLower(filepath.Ext(name)) == ".md" {
b = github_flavored_markdown.Markdown(b)
// unescape quotes so raymond can parse the file correctly.
b = bytes.Replace(b, []byte("""), []byte("\""), -1)
}
source := string(b)
t, err = raymond.Parse(source)
if err != nil {
return t, errors.Errorf("Error parsing %s: %+v", name, errors.WithStack(err))
}
t.RegisterHelpers(s.Helpers)
if s.CacheTemplates {
s.templateCache[name] = t
}
return t.Clone(), 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,
}
}