a7791c27 by astaxie

#2

#2
1 parent e81795a0
...@@ -30,7 +30,7 @@ Here is the canonical "Hello, world" example app for beego: ...@@ -30,7 +30,7 @@ Here is the canonical "Hello, world" example app for beego:
30 } 30 }
31 31
32 func (this *MainController) Get() { 32 func (this *MainController) Get() {
33 this.Ct.WriteString("hello world") 33 this.Ctx.WriteString("hello world")
34 } 34 }
35 35
36 func main() { 36 func main() {
...@@ -177,10 +177,10 @@ So you can define ChildStruct method to accomplish the interface's method, now l ...@@ -177,10 +177,10 @@ So you can define ChildStruct method to accomplish the interface's method, now l
177 177
178 func (this *AddController) Post() { 178 func (this *AddController) Post() {
179 //data deal with 179 //data deal with
180 this.Ct.Request.ParseForm() 180 this.Ctx.Request.ParseForm()
181 pkgname := this.Ct.Request.Form.Get("pkgname") 181 pkgname := this.Ctx.Request.Form.Get("pkgname")
182 content := this.Ct.Request.Form.Get("content") 182 content := this.Ctx.Request.Form.Get("content")
183 beego.Info(this.Ct.Request.Form) 183 beego.Info(this.Ctx.Request.Form)
184 pk := models.GetCruPkg(pkgname) 184 pk := models.GetCruPkg(pkgname)
185 if pk.Id == 0 { 185 if pk.Id == 0 {
186 var pp models.PkgEntity 186 var pp models.PkgEntity
...@@ -194,7 +194,7 @@ So you can define ChildStruct method to accomplish the interface's method, now l ...@@ -194,7 +194,7 @@ So you can define ChildStruct method to accomplish the interface's method, now l
194 at.Pkgid = pk.Id 194 at.Pkgid = pk.Id
195 at.Content = content 195 at.Content = content
196 models.InsertArticle(at) 196 models.InsertArticle(at)
197 this.Ct.Redirect(302, "/admin/index") 197 this.Ctx.Redirect(302, "/admin/index")
198 } 198 }
199 199
200 ## View / Template 200 ## View / Template
...@@ -216,7 +216,7 @@ then beego will find the file in the path:`/views/admin/add.tpl` ...@@ -216,7 +216,7 @@ then beego will find the file in the path:`/views/admin/add.tpl`
216 216
217 if you don't set TplNames,beego will find like this: 217 if you don't set TplNames,beego will find like this:
218 218
219 c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt 219 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
220 220
221 So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl" 221 So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl"
222 So beego will file the file in the path:`/view/AddController/POST.tpl` 222 So beego will file the file in the path:`/view/AddController/POST.tpl`
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
13 ) 13 )
14 14
15 type Controller struct { 15 type Controller struct {
16 Ct *Context 16 Ctx *Context
17 Tpl *template.Template 17 Tpl *template.Template
18 Data map[interface{}]interface{} 18 Data map[interface{}]interface{}
19 ChildName string 19 ChildName string
...@@ -36,14 +36,14 @@ type ControllerInterface interface { ...@@ -36,14 +36,14 @@ type ControllerInterface interface {
36 Render() error 36 Render() error
37 } 37 }
38 38
39 func (c *Controller) Init(ct *Context, cn string) { 39 func (c *Controller) Init(ctx *Context, cn string) {
40 c.Data = make(map[interface{}]interface{}) 40 c.Data = make(map[interface{}]interface{})
41 c.Tpl = template.New(cn + ct.Request.Method) 41 c.Tpl = template.New(cn + ctx.Request.Method)
42 c.Tpl = c.Tpl.Funcs(beegoTplFuncMap) 42 c.Tpl = c.Tpl.Funcs(beegoTplFuncMap)
43 c.Layout = "" 43 c.Layout = ""
44 c.TplNames = "" 44 c.TplNames = ""
45 c.ChildName = cn 45 c.ChildName = cn
46 c.Ct = ct 46 c.Ctx = ctx
47 c.TplExt = "tpl" 47 c.TplExt = "tpl"
48 48
49 } 49 }
...@@ -57,38 +57,38 @@ func (c *Controller) Finish() { ...@@ -57,38 +57,38 @@ func (c *Controller) Finish() {
57 } 57 }
58 58
59 func (c *Controller) Get() { 59 func (c *Controller) Get() {
60 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 60 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
61 } 61 }
62 62
63 func (c *Controller) Post() { 63 func (c *Controller) Post() {
64 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 64 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
65 } 65 }
66 66
67 func (c *Controller) Delete() { 67 func (c *Controller) Delete() {
68 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 68 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
69 } 69 }
70 70
71 func (c *Controller) Put() { 71 func (c *Controller) Put() {
72 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 72 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
73 } 73 }
74 74
75 func (c *Controller) Head() { 75 func (c *Controller) Head() {
76 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 76 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
77 } 77 }
78 78
79 func (c *Controller) Patch() { 79 func (c *Controller) Patch() {
80 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 80 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
81 } 81 }
82 82
83 func (c *Controller) Options() { 83 func (c *Controller) Options() {
84 http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) 84 http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
85 } 85 }
86 86
87 func (c *Controller) Render() error { 87 func (c *Controller) Render() error {
88 //if the controller has set layout, then first get the tplname's content set the content to the layout 88 //if the controller has set layout, then first get the tplname's content set the content to the layout
89 if c.Layout != "" { 89 if c.Layout != "" {
90 if c.TplNames == "" { 90 if c.TplNames == "" {
91 c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt 91 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
92 } 92 }
93 t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout)) 93 t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout))
94 if err != nil { 94 if err != nil {
...@@ -100,20 +100,20 @@ func (c *Controller) Render() error { ...@@ -100,20 +100,20 @@ func (c *Controller) Render() error {
100 tplcontent, _ := ioutil.ReadAll(newbytes) 100 tplcontent, _ := ioutil.ReadAll(newbytes)
101 c.Data["LayoutContent"] = template.HTML(string(tplcontent)) 101 c.Data["LayoutContent"] = template.HTML(string(tplcontent))
102 _, file = path.Split(c.Layout) 102 _, file = path.Split(c.Layout)
103 err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data) 103 err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data)
104 if err != nil { 104 if err != nil {
105 Trace("template Execute err:", err) 105 Trace("template Execute err:", err)
106 } 106 }
107 } else { 107 } else {
108 if c.TplNames == "" { 108 if c.TplNames == "" {
109 c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt 109 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
110 } 110 }
111 t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames)) 111 t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames))
112 if err != nil { 112 if err != nil {
113 Trace("template ParseFiles err:", err) 113 Trace("template ParseFiles err:", err)
114 } 114 }
115 _, file := path.Split(c.TplNames) 115 _, file := path.Split(c.TplNames)
116 err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data) 116 err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data)
117 if err != nil { 117 if err != nil {
118 Trace("template Execute err:", err) 118 Trace("template Execute err:", err)
119 } 119 }
...@@ -122,32 +122,32 @@ func (c *Controller) Render() error { ...@@ -122,32 +122,32 @@ func (c *Controller) Render() error {
122 } 122 }
123 123
124 func (c *Controller) Redirect(url string, code int) { 124 func (c *Controller) Redirect(url string, code int) {
125 c.Ct.Redirect(code, url) 125 c.Ctx.Redirect(code, url)
126 } 126 }
127 127
128 func (c *Controller) ServeJson() { 128 func (c *Controller) ServeJson() {
129 content, err := json.MarshalIndent(c.Data, "", " ") 129 content, err := json.MarshalIndent(c.Data, "", " ")
130 if err != nil { 130 if err != nil {
131 http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError) 131 http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
132 return 132 return
133 } 133 }
134 c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true) 134 c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
135 c.Ct.ContentType("json") 135 c.Ctx.ContentType("json")
136 c.Ct.ResponseWriter.Write(content) 136 c.Ctx.ResponseWriter.Write(content)
137 } 137 }
138 138
139 func (c *Controller) ServeXml() { 139 func (c *Controller) ServeXml() {
140 content, err := xml.Marshal(c.Data) 140 content, err := xml.Marshal(c.Data)
141 if err != nil { 141 if err != nil {
142 http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError) 142 http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
143 return 143 return
144 } 144 }
145 c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true) 145 c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
146 c.Ct.ContentType("xml") 146 c.Ctx.ContentType("xml")
147 c.Ct.ResponseWriter.Write(content) 147 c.Ctx.ResponseWriter.Write(content)
148 } 148 }
149 149
150 func (c *Controller) Input() url.Values { 150 func (c *Controller) Input() url.Values {
151 c.Ct.Request.ParseForm() 151 c.Ctx.Request.ParseForm()
152 return c.Ct.Request.Form 152 return c.Ctx.Request.Form
153 } 153 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!