-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
47 lines (41 loc) · 1.04 KB
/
render.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
package restful
import (
"github.com/cockroachdb/errors"
"github.com/gin-gonic/gin/render"
"html/template"
)
// NewRender returns a fresh instance of Render
func NewRender() *Render {
return &Render{
templates: make(map[string]*template.Template),
Files: make(map[string][]string),
}
}
// Render implements gin's HTMLRender and provides some sugar on top of it
type Render struct {
templates map[string]*template.Template
Files map[string][]string
TemplatesDir string
Layout string
Ext string
Debug bool
}
// Add assigns the name to the template
func (r *Render) Add(name string, tmpl *template.Template) error {
if tmpl == nil {
return errors.New("template can not be nil")
}
if len(name) == 0 {
return errors.New("template name cannot be empty")
}
r.templates[name] = tmpl
return nil
}
// Instance implements gin's HTML render interface
func (r *Render) Instance(name string, data interface{}) render.Render {
tpl := r.templates[name]
return &render.HTML{
Template: tpl,
Data: data,
}
}