-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathview.go
69 lines (56 loc) · 1.34 KB
/
view.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2012 by sdm. All rights reserved.
// license that can be found in the LICENSE file.
package wk
import (
"bytes"
"mime"
"path/filepath"
)
// ViewResult
type ViewResult struct {
File string
}
// View return *ViewResult
func View(file string) *ViewResult {
return &ViewResult{
File: file,
}
}
// String
func (v *ViewResult) String() string {
return v.File
}
func executeViewFile(file string, ctx *HttpContext) error {
view := &ViewResult{
File: file,
}
return view.Execute(ctx)
}
// Execute
func (v *ViewResult) Execute(ctx *HttpContext) error {
buffer := &bytes.Buffer{}
err := ctx.Server.ViewEngine.Execte(buffer, v.File, ctx.ViewData)
if err != nil {
return err
}
ctx.SetHeader(HeaderContentType, v.Type())
//ctx.SetHeader(HeaderContentLength, strconv.Itoa(len(buffer.Bytes())))
_, err = buffer.WriteTo(ctx.Resonse)
return err
}
// ContentType return mime type
func (v *ViewResult) Type() string {
ctype := mime.TypeByExtension(filepath.Ext(v.File))
return ctype
}
// // Write execute template and write output to body
// func (v *ViewResult) Write(header http.Header, body io.Writer) error {
// buffer := &bytes.Buffer{}
// err := DefaultViewEngine.Execte(buffer, v.File, v.Data)
// if err != nil {
// return err
// }
// header.Set(HeaderContentType, v.Type())
// _, err = buffer.WriteTo(body)
// return err
// }