forked from yaitoo/xun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer_text.go
More file actions
40 lines (32 loc) · 1.12 KB
/
Copy pathviewer_text.go
File metadata and controls
40 lines (32 loc) · 1.12 KB
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
package xun
import (
"net/http"
)
func NewTextViewer(t *TextTemplate) *TextViewer {
return &TextViewer{template: t}
}
// TextViewer is a struct that holds an TextTemplate and is used to render text content.
type TextViewer struct {
template *TextTemplate
}
// MimeType returns the MIME type for the text content rendered by the TextViewer.
func (v *TextViewer) MimeType() *MimeType {
return &v.template.mime
}
// Render writes the text content rendered by the TextViewer to the provided http.ResponseWriter.
// It sets the Content-Type header to "text/plain; charset=utf-8" and writes the rendered content to the response.
// If there is an error executing the template, it is returned.
func (v *TextViewer) Render(ctx *Context, data any) error { // skipcq: RVV-B0012
var err error
ctx.Response.Header().Set("Content-Type", v.template.mime.String()+v.template.charset)
if ctx.Request.Method != http.MethodHead {
buf := BufPool.Get()
defer BufPool.Put(buf)
err = v.template.Execute(buf, ViewModel{TempData: ctx.TempData, Data: data})
if err != nil {
return err
}
_, err = buf.WriteTo(ctx.Response)
}
return err
}