-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviewer_html.go
51 lines (43 loc) · 1.45 KB
/
viewer_html.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 xun
import (
"net/http"
)
// BufPool is a pool of *bytes.Buffer for reuse to reduce memory alloc.
//
// It is used by the HtmlViewer to render the template.
// The pool is created with a size of 100, but you can change it by setting the
// BufPool variable before creating any HtmlViewer instances.
var BufPool *BufferPool
func init() {
BufPool = NewBufferPool(100)
}
// HtmlViewer is a viewer that renders a html template.
//
// It uses the `HtmlTemplate` type to render a template.
// The template is loaded from the file system when the viewer is created.
// The `Render` method renders the template with the given data and writes the
// result to the http.ResponseWriter.
type HtmlViewer struct {
template *HtmlTemplate
}
// MimeType returns the MIME type of the HTML content.
//
// This implementation returns "text/html".
func (*HtmlViewer) MimeType() string {
return "text/html"
}
// Render renders the template with the given data and writes the result to the http.ResponseWriter.
//
// This implementation uses the `HtmlTemplate.Execute` method to render the template.
// The rendered result is written to the http.ResponseWriter.
func (v *HtmlViewer) Render(w http.ResponseWriter, r *http.Request, data any) error { //skipcq: RVV-B0012
w.Header().Add("Content-Type", "text/html; charset=utf-8")
buf := BufPool.Get()
defer BufPool.Put(buf)
err := v.template.Execute(buf, data)
if err != nil {
return err
}
_, err = buf.WriteTo(w)
return err
}