53ce6ad3 by xiemengjun

add more intro

1 parent 1318f88e
Showing 1 changed file with 159 additions and 0 deletions
...@@ -100,9 +100,168 @@ also You can apply filters only when certain prefix URL path exist: ...@@ -100,9 +100,168 @@ also You can apply filters only when certain prefix URL path exist:
100 100
101 ## Controller / Strcut 101 ## Controller / Strcut
102 ============ 102 ============
103 you type a ChildStruct has anonymous type `beego.Controller`
104
105 type xxxController struct {
106 beego.Controller
107 }
108
109 the `beego.Controller` is `beego.ControllerInterface` has the follow method:
110
111 - Init(ct *Context, cn string)
112
113 this function is init the Context, ChildStruct' name and the Controller's variables.
114
115 - Prepare()
116
117 this function is Run before the HTTP METHOD's Function,as follow defined. In the ChildStruct you can define this function to auth user or init database et.
118
119 - Get()
120
121 When the HTTP' Method is GET, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
122
123 - Post()
124
125 When the HTTP' Method is POST, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
126
127 - Delete()
128
129 When the HTTP' Method is DELETE, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
130
131 - Put()
132
133 When the HTTP' Method is PUT, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
134
135 - Head()
136
137 When the HTTP' Method is HEAD, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
138
139 - Patch()
140
141 When the HTTP' Method is PATCH, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
142
143 - Options()
144
145 When the HTTP' Method is OPTIONS, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
146
147 - Finish()
148
149 this function is run after the HTTP METHOD's Function,as previous defined. In the ChildStruct you can define this function to close database et.
150
151 - Render() error
152
153 this function is to render the template as user defined. In the strcut you need to call.
154
155
156 So you can define ChildStruct method to accomplish the interface's method, now let us see an example:
157
158 type AddController struct {
159 beego.Controller
160 }
161
162 func (this *AddController) Prepare() {
163
164 }
165
166 func (this *AddController) Get() {
167 this.Layout = "admin/layout.html"
168 this.TplNames = "admin/add.tpl"
169 }
170
171 func (this *AddController) Post() {
172 //数据处理
173 this.Ct.Request.ParseForm()
174 pkgname := this.Ct.Request.Form.Get("pkgname")
175 content := this.Ct.Request.Form.Get("content")
176 beego.Info(this.Ct.Request.Form)
177 pk := models.GetCruPkg(pkgname)
178 if pk.Id == 0 {
179 var pp models.PkgEntity
180 pp.Pid = 0
181 pp.Pathname = pkgname
182 pp.Intro = pkgname
183 models.InsertPkg(pp)
184 pk = models.GetCruPkg(pkgname)
185 }
186 var at models.Article
187 at.Pkgid = pk.Id
188 at.Content = content
189 models.InsertArticle(at)
190 this.Ct.Redirect(302, "/admin/index")
191 }
103 192
104 ## View / Template 193 ## View / Template
105 ============ 194 ============
195 ### template view path
196
197 the default viewPath is `/views`,you can put the template file in the views.beego will find the template from viewpath.
198
199 also you can modify the viewpaths like this:
200
201 beego.ViewsPath = "/myviewpath"
202
203 ### template names
204 beego will find the template from viewpath. the file is set by user like:
205
206 this.TplNames = "admin/add.tpl"
207
208 then beego will find the file in the path:`/views/admin/add.tpl`
209
210 if you don't set TplNames,beego will find like this:
211
212 c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt
213
214 So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl"
215 So beego will file the file in the path:`/view/AddController/POST.tpl`
216
217 ### autoRender
218 In the controller you needn't to call render function. beego will auto call this function after HTTP' Method Call.
219
220 also you can close the autoRendder like this:
221
222 beego.AutoRender = false
223
224
225 ### layout
226 beego also support layout. beego's layout is like this:
227
228 this.Layout = "admin/layout.html"
229 this.TplNames = "admin/add.tpl"
230
231 in the layout.html you must define the variable like this to show sub template's content:
232
233 {{.LayoutContent}}
234
235 beego first Parse the file TplNames defined, then get the content from the sub template to the data["LayoutContent"], at last Parse the layout file and show it.
236
237 ### template function
238 beego support users to define template function like this:
239
240 func hello(in string)(out string){
241 out = in + "world"
242 return
243 }
244
245 beego.AddFuncMap("hi",hello)
246
247 then in you template you can use it like this:
248
249 {{.Content | hi}}
250
251 beego has three default defined funtion:
252
253 - beegoTplFuncMap["markdown"] = MarkDown
254
255 MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown"
256
257 - beegoTplFuncMap["dateformat"] = DateFormat
258
259 DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat"
260
261 - beegoTplFuncMap["compare"] = Compare
262
263 Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal.Whitespace is trimmed. Used by the template parser as "eq"
264
106 265
107 ## Config 266 ## Config
108 ============ 267 ============
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!