-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.go
More file actions
37 lines (31 loc) · 756 Bytes
/
view.go
File metadata and controls
37 lines (31 loc) · 756 Bytes
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
package html
import (
"bytes"
"html/template"
"io"
)
// A View can be rendered to HTML text.
type View struct {
set *Set
template *template.Template
}
// Write applies the parsed template to the specified data object,
// writing the output to wr.
func (v *View) Write(wr io.Writer, data ...interface{}) error {
err := v.set.create(v)
if err != nil {
return err
}
var input interface{}
if len(data) != 0 {
input = data[0]
}
return v.template.ExecuteTemplate(wr, rootTemplateName, input)
}
// HTML applies the parsed template to the specified data object,
// returning the result as a text.
func (v *View) HTML(data ...interface{}) (string, error) {
var doc bytes.Buffer
err := v.Write(&doc, data...)
return doc.String(), err
}