forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.go
68 lines (60 loc) · 2.18 KB
/
js.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
package render
import (
"html/template"
"strings"
"github.com/gobuffalo/plush"
"github.com/pkg/errors"
)
// JavaScript renders the named files using the 'application/javascript'
// content type and the github.com/gobuffalo/plush
// 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 JavaScript(names ...string) Renderer {
e := New(Options{})
return e.JavaScript(names...)
}
// JavaScript renders the named files using the 'application/javascript'
// content type and the github.com/gobuffalo/plush
// 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 %>". If no
// second file is provided and an `JavaScriptLayout` is specified
// in the options, then that layout file will be used
// automatically.
func (e *Engine) JavaScript(names ...string) Renderer {
if e.JavaScriptLayout != "" && len(names) == 1 {
names = append(names, e.JavaScriptLayout)
}
hr := templateRenderer{
Engine: e,
contentType: "application/javascript",
names: names,
}
return hr
}
// JSTemplateEngine renders files with a `.js` extension through Plush.
// It also implements a new `partial` helper that will run non-JS partials
// through `JSEscapeString` before injecting.
func JSTemplateEngine(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error) {
var pf partFunc
var ok bool
if pf, ok = helpers["partial"].(func(string, Data) (template.HTML, error)); !ok {
return "", errors.New("could not find a partial function")
}
helpers["partial"] = func(name string, dd Data) (template.HTML, error) {
if strings.Contains(name, ".js") {
return pf(name, dd)
}
h, err := pf(name, dd)
if err != nil {
return "", errors.WithStack(err)
}
he := template.JSEscapeString(string(h))
return template.HTML(he), nil
}
return plush.BuffaloRenderer(input, data, helpers)
}
type partFunc func(string, Data) (template.HTML, error)