This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
58 lines (50 loc) · 1.73 KB
/
context.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
package congo
import (
"html/template"
"net/http"
)
type Context interface {
ResponseWriter() http.ResponseWriter
Request() *http.Request
Content() template.HTML
Write([]byte) (int, error)
}
type BaseContext struct {
responseWriter http.ResponseWriter
request *http.Request
// content holds intermediate template results. In Congo, the convention
// (as in many large web frameworks) is to render a template specific to the
// current action then inject it's result into a layout template that is
// generally common to many actions. This value is used to store the
// rendered action template then passed to the layout template which will
// call {{.Content}} to insert the action template result into the layout.
// This is similar to Rails' <%= yield %>.
content string
}
func NewBaseContext(w http.ResponseWriter, r *http.Request) *BaseContext {
return &BaseContext{
responseWriter: w,
request: r,
}
}
// Returns the current response writer associated with the context.
func (c *BaseContext) ResponseWriter() http.ResponseWriter {
return c.responseWriter
}
// Returns the current request associated with the context.
func (c *BaseContext) Request() *http.Request {
return c.request
}
// Returns the currently defined content. In action templates this will be
// not be set and is really only of interest when injecting action template
// results into layouts.
func (c *BaseContext) Content() template.HTML {
return template.HTML(c.content)
}
// This is used to implement the Writer interface so that we can render the
// action template directly into the context for later use in layout
// templates.
func (c *BaseContext) Write(data []byte) (int, error) {
c.content = c.content + string(data)
return len(data), nil
}