forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
51 lines (44 loc) · 1.04 KB
/
string.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
package render
import (
"fmt"
"io"
"github.com/pkg/errors"
)
type stringRenderer struct {
*Engine
body string
}
func (s stringRenderer) ContentType() string {
return "text/plain; charset=utf-8"
}
func (s stringRenderer) Render(w io.Writer, data Data) error {
te, ok := s.TemplateEngines["text"]
if !ok {
return errors.New("could not find a template engine for text")
}
t, err := te(s.body, data, s.Helpers)
if err != nil {
return err
}
_, err = w.Write([]byte(t))
return err
}
// String renderer that will run the string through
// the github.com/gobuffalo/plush package and return
// "text/plain" as the content type.
func String(s string, args ...interface{}) Renderer {
e := New(Options{})
return e.String(s, args...)
}
// String renderer that will run the string through
// the github.com/gobuffalo/plush package and return
// "text/plain" as the content type.
func (e *Engine) String(s string, args ...interface{}) Renderer {
if len(args) > 0 {
s = fmt.Sprintf(s, args...)
}
return stringRenderer{
Engine: e,
body: s,
}
}