forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
130 lines (118 loc) · 3.42 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
126
127
128
129
130
package render
import (
"bytes"
"fmt"
"html/template"
"io"
"path/filepath"
"strings"
"github.com/gobuffalo/velvet"
"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 template.HTML
var err error
for _, name := range s.names {
yield, err = s.execute(name, data.ToVelvet())
if err != nil {
err = errors.Errorf("error rendering %s:\n%+v", name, err)
return err
}
data["yield"] = yield
}
_, err = w.Write([]byte(yield))
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (s *templateRenderer) execute(name string, data *velvet.Context) (template.HTML, error) {
source, err := s.source(name)
if err != nil {
return "", err
}
err = source.Helpers.Add("partial", func(name string, help velvet.HelperContext) (template.HTML, error) {
p, err := s.partial(name, help.Context)
if err != nil {
return template.HTML(fmt.Sprintf("<pre>%s: %s</pre>", name, err.Error())), err
}
return p, nil
})
if err != nil {
return template.HTML(fmt.Sprintf("<pre>%s: %s</pre>", name, err.Error())), err
}
yield, err := source.Exec(data)
if err != nil {
return template.HTML(fmt.Sprintf("<pre>%s: %s</pre>", name, err.Error())), err
}
return template.HTML(yield), nil
}
func (s *templateRenderer) source(name string) (*velvet.Template, error) {
var t *velvet.Template
var ok bool
var err error
if s.CacheTemplates {
if t, ok = s.templateCache[name]; ok {
return t.Clone(), nil
}
}
b, err := s.Resolver().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 = velvet.Parse(source)
if err != nil {
return t, errors.Errorf("Error parsing %s: %+v", name, errors.WithStack(err))
}
err = t.Helpers.AddMany(s.Helpers)
if err != nil {
return nil, err
}
if s.CacheTemplates {
s.templateCache[name] = t
}
return t.Clone(), err
}
func (s *templateRenderer) partial(name string, data *velvet.Context) (template.HTML, 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,
}
}