068786d0 by Unknown

incomplete quickstart: template, handle request

1 parent 6c56a73b
...@@ -10,8 +10,8 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't ...@@ -10,8 +10,8 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't
10 - [Static files](#static-files) 10 - [Static files](#static-files)
11 - [Filter and middleware](#filter-and-middleware) 11 - [Filter and middleware](#filter-and-middleware)
12 - [Controller](#controller) 12 - [Controller](#controller)
13 - [Template](#-8) 13 - [Template](#template)
14 - [Handle request](#request) 14 - [Handle request](#handle-request)
15 - [Redirect and error](#-15) 15 - [Redirect and error](#-15)
16 - [Handle response](#response) 16 - [Handle response](#response)
17 - [Sessions](#sessions) 17 - [Sessions](#sessions)
...@@ -290,64 +290,65 @@ Overload all methods for all customized logic processes, let's see an example: ...@@ -290,64 +290,65 @@ Overload all methods for all customized logic processes, let's see an example:
290 this.Ctx.Redirect(302, "/admin/index") 290 this.Ctx.Redirect(302, "/admin/index")
291 } 291 }
292 292
293 ## 模板处理 293 ##Template
294 ### 模板目录 294 ###Template directory
295 beego中默认的模板目录是`views`,用户可以把你的模板文件放到该目录下,beego会自动在该目录下的所有模板文件进行解析并缓存,开发模式下会每次重新解析,不做缓存。当然用户可以通过如下的方式改变模板的目录: 295 Beego uses `views` as the default directory for template files, parses and caches them as needed(cache is not enable in develop mode), but you can **change**(because only one directory can be used for template files) its directory using following code:
296 296
297 beego.ViewsPath = "/myviewpath" 297 beego.ViewsPath = "/myviewpath"
298 ### 自动渲染
299 beego中用户无需手动的调用渲染输出模板,beego会自动的在调用玩相应的method方法之后调用Render函数,当然如果你的应用是不需要模板输出的,那么你可以在配置文件或者在main.go中设置关闭自动渲染。
300 298
301 配置文件配置如下: 299 ###Auto-render
300 You don't need to call render function manually, Beego calls it automatically after corresponding methods executed. If your application is somehow doesn't need templates, you can disable this feature either in code of `main.go` or configuration file.
301
302 To disable auto-render in configuration file:
302 303
303 autorender = false 304 autorender = false
304 305
305 main.go文件中设置如下: 306 To disable auto-render in `main.go`(before you call `beego.Run()` to run the application):
306 307
307 beego.AutoRender = false 308 beego.AutoRender = false
308 309
309 ### 模板数据 310 ###Template data
310 模板中的数据是通过在Controller中`this.Data`获取的,所以如果你想在模板中获取内容`{{.Content}}`,那么你需要在Controller中如下设置: 311 You can use `this.Data` in controller methods to access the data in templates. Suppose you want to get content of `{{.Content}}`, you can use following code to do this:
311 312
312 this.Data["Context"] = "value" 313 this.Data["Context"] = "value"
313 314
314 ### 模板名称 315 ###Template name
315 beego采用了Go语言内置的模板引擎,所有模板的语法和Go的一模一样,至于如何写模板文件,详细的请参考[模板教程](https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.4.md) 316 Beego uses built-in template engine of Go, so there is no different in syntax. As for how to write template file, please visit [Template tutorial](https://github.com/Unknwon/build-web-application-with-golang_EN/blob/master/eBook/07.4.md)
316 317
317 用户通过在Controller的对应方法中设置相应的模板名称,beego会自动的在viewpath目录下查询该文件并渲染,例如下面的设置,beego会在admin下面找add.tpl文件进行渲染: 318 Beego parses template files in `viewpath` and render it after you set the name of the template file in controller methods. For example, Beego finds the file `add.tpl` in directory `admin` in following code:
318 319
319 this.TplNames = "admin/add.tpl" 320 this.TplNames = "admin/add.tpl"
320 321
321 我们看到上面的模板后缀名是tpl,beego默认情况下支持tpl和html后缀名的模板文件,如果你的后缀名不是这两种,请进行如下设置: 322 Beego supports two kinds of extensions for template files, which are `tpl` and `html`, if you want to use other extensions, you have to use following code to let Beego know:
322 323
323 beego.AddTemplateExt("你文件的后缀名") 324 beego.AddTemplateExt("<your template file extension>")
324 325
325 当你设置了自动渲染,然后在你的Controller中没有设置任何的TplNames,那么beego会自动设置你的模板文件如下: 326 If you enabled auto-render and you don't tell Beego which template file you are going to use in controller methods, Beego uses following format to find the template file if it exists:
326 327
327 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt 328 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
328 329
329 也就是你对应的Controller名字+请求方法名.模板后缀,也就是如果你的Controller名是`AddController`,请求方法是`POST`,默认的文件后缀是`tpl`,那么就会默认请求`/viewpath/AddController/POST.tpl`文件。 330 Which is `<corresponding controller name>/<request method name>.<template extension>`. For example, your controller name is `AddController` and the request method is POST, and the default file extension is `tpl`, so Beego will try to find file `/<viewpath>/AddController/POST.tpl`.
330 331
331 ### lauout设计 332 ###Layout design
332 beego支持layout设计,例如你在管理系统中,其实整个的管理界面是固定的,支会变化中间的部分,那么你可以通过如下的设置: 333 Beego supports layout design, which means if you are working on an administration application, and some part of its user interface is exactly same all the time, then you can make this part as a layout.
333 334
334 this.Layout = "admin/layout.html" 335 this.Layout = "admin/layout.html"
335 this.TplNames = "admin/add.tpl" 336 this.TplNames = "admin/add.tpl"
336 337
337 在layout.html中你必须设置如下的变量: 338 You have to set following variable in order to make Beego possible to insert your dynamic content:
338 339
339 {{.LayoutContent}} 340 {{.LayoutContent}}
340 341
341 beego就会首先解析TplNames指定的文件,获取内容赋值给LayoutContent,然后最后渲染layout.html文件。 342 Beego parses template file and assign content to `LayoutContent`, and render them together.
342 343
343 目前采用首先把目录下所有的文件进行缓存,所以用户还可以通过类似这样的方式实现layout: 344 Right now, Beego caches all template files, so you can use following way to implement another kind of layout:
344 345
345 {{template "header.html"}} 346 {{template "header.html"}}
346 处理逻辑 347 Handle logic
347 {{template "footer.html"}} 348 {{template "footer.html"}}
348 349
349 ### 模板函数 350 ###Template function
350 beego支持用户定义模板函数,但是必须在`beego.Run()`调用之前,设置如下: 351 Beego supports customized template functions that are registered before you call `beego.Run()`.
351 352
352 func hello(in string)(out string){ 353 func hello(in string)(out string){
353 out = in + "world" 354 out = in + "world"
...@@ -356,48 +357,48 @@ beego支æŒç”¨æˆ·å®šä¹‰æ¨¡æ¿å‡½æ•°ï¼Œä½†æ˜¯å¿…须在`beego.Run()`调用之å‰ï¼ ...@@ -356,48 +357,48 @@ beego支æŒç”¨æˆ·å®šä¹‰æ¨¡æ¿å‡½æ•°ï¼Œä½†æ˜¯å¿…须在`beego.Run()`调用之å‰ï¼
356 357
357 beego.AddFuncMap("hi",hello) 358 beego.AddFuncMap("hi",hello)
358 359
359 定义之后你就可以在模板中这样使用了: 360 Then you can use this function in your template files:
360 361
361 {{.Content | hi}} 362 {{.Content | hi}}
362 363
363 目前beego内置的模板函数有如下: 364 There are some built-in template functions:
364 365
365 * markdown 366 * markdown
366 367
367 实现了把markdown文本转化为html信息,使用方法{{markdown .Content}} 368 This function converts markdown content to HTML format, use {{markdown .Content}} in template files.
368 * dateformat 369 * dateformat
369 370
370 实现了时间的格式化,返回字符串,使用方法{{dateformat .Time "2006-01-02T15:04:05Z07:00"}} 371 This function converts time to formatted string, use {{dateformat .Time "2006-01-02T15:04:05Z07:00"}} in template files.
371 * date 372 * date
372 373
373 实现了类似PHP的date函数,可以很方便的根据字符串返回时间,使用方法{{date .T "Y-m-d H:i:s"}} 374 This function implements date function like in PHP, use formatted string to get corresponding time, use {{date .T "Y-m-d H:i:s"}} in template files.
374 * compare 375 * compare
375 376
376 实现了比较两个对象的比较,如果相同返回true,否者false,使用方法{{compare .A .B}} 377 This functions compares two objects, returns true if they are same, false otherwise, use {{compare .A .B}} in template files.
377 * substr 378 * substr
378 379
379 实现了字符串的截取,支持中文截取的完美截取,使用方法{{substr .Str 0 30}} 380 This function cuts out string from another string by index, it supports UTF-8 characters, use {{substr .Str 0 30}} in template files.
380 * html2str 381 * html2str
381 382
382 实现了把html转化为字符串,剔除一些script、css之类的元素,返回纯文本信息,使用方法{{html2str .Htmlinfo}} 383 This function escapes HTML to raw string, use {{html2str .Htmlinfo}} in template files.
383 * str2html 384 * str2html
384 385
385 实现了把相应的字符串当作HTML来输出,不转义,使用方法{{str2html .Strhtml}} 386 This function outputs string in HTML format without escaping, use {{str2html .Strhtml}} in template files.
386 * htmlquote 387 * htmlquote
387 388
388 实现了基本的html字符转义,使用方法{{htmlquote .quote}} 389 This functions implements basic HTML escape, use {{htmlquote .quote}} in template files.
389 * htmlunquote 390 * htmlunquote
390 391
391 实现了基本的反转移字符,使用方法{{htmlunquote .unquote}} 392 This functions implements basic invert-escape of HTML, use {{htmlunquote .unquote}} in template files.
392 393
393 ## request处理 394 ##Handle request
394 我们经常需要获取用户传递的数据,包括Get、POST等方式的请求,beego里面会自动解析这些数据,你可以通过如下方式获取数据 395 We always need to get data from users, including methods like GET, POST, etc. Beego parses these data automatically, and you can access them by following code:
395 396
396 - GetString(key string) string 397 - GetString(key string) string
397 - GetInt(key string) (int64, error) 398 - GetInt(key string) (int64, error)
398 - GetBool(key string) (bool, error) 399 - GetBool(key string) (bool, error)
399 400
400 使用例子如下: 401 Usage example:
401 402
402 func (this *MainController) Post() { 403 func (this *MainController) Post() {
403 jsoninfo := this.GetString("jsoninfo") 404 jsoninfo := this.GetString("jsoninfo")
...@@ -407,46 +408,48 @@ beego支æŒç”¨æˆ·å®šä¹‰æ¨¡æ¿å‡½æ•°ï¼Œä½†æ˜¯å¿…须在`beego.Run()`调用之å‰ï¼ ...@@ -407,46 +408,48 @@ beego支æŒç”¨æˆ·å®šä¹‰æ¨¡æ¿å‡½æ•°ï¼Œä½†æ˜¯å¿…须在`beego.Run()`调用之å‰ï¼
407 } 408 }
408 } 409 }
409 410
410 如果你需要的数据可能是其他类型的,例如是int类型而不是int64,那么你需要这样处理: 411 If you need other types that are not included above, like you need int64 instead of int, then you need to do following way:
411 412
412 func (this *MainController) Post() { 413 func (this *MainController) Post() {
413 id := this.Input().Get("id") 414 id := this.Input().Get("id")
414 intid, err := strconv.Atoi(id) 415 intid, err := strconv.Atoi(id)
415 } 416 }
416 417
417 更多其他的request的信息,用户可以通过`this.Ctx.Request`获取信息,关于该对象的属性和方法参考手册[Request](http://golang.org/pkg/net/http/#Request) 418 To use `this.Ctx.Request` for more information about request, and object properties and method please read [Request](http://golang.org/pkg/net/http/#Request)
419
420 ###File upload
421 It's very easy to upload file through Beego, but don't forget to add `enctype="multipart/form-data"` in your form, otherwise the browser will not upload anything.
418 422
419 ### 文件上传 423 Files will be saved in memory, if the size is greater than cache memory, the rest part will be saved as temporary file. The default cache memory is 64 MB, and you can using following ways to change this size.
420 在beego中你可以很容易的处理文件上传,就是别忘记在你的form表单中增加这个属性`enctype="multipart/form-data"`,否者你的浏览器不会传输你的上传文件。
421 424
422 文件上传之后一般是放在系统的内存里面,如果文件的size大于设置的缓存内存大小,那么就放在临时文件中,默认的缓存内存是64M,你可以通过如下来调整这个缓存内存大小: 425 In code:
423 426
424 beego.MaxMemory = 1<<22 427 beego.MaxMemory = 1<<22
425 428
426 或者在配置文件中通过如下设置 429 In configuration file:
427 430
428 maxmemory = 1<<22 431 maxmemory = 1<<22
429 432
430 beego提供了两个很方便的方法来处理文件上传: 433 Beego provides two convenient functions to upload files:
431 434
432 - GetFile(key string) (multipart.File, *multipart.FileHeader, error) 435 - GetFile(key string) (multipart.File, `*`multipart.FileHeader, error)
433 436
434 该方法主要用于用户读取表单中的文件名`the_file`,然后返回相应的信息,用户根据这些变量来处理文件上传:过滤、保存文件等。 437 This function is mainly used to read file name element `the_file` in form and returns corresponding information. You can use this information either filter or save files.
435 438
436 - SaveToFile(fromfile, tofile string) error 439 - SaveToFile(fromfile, tofile string) error
437 440
438 该方法是在GetFile的基础上实现了快速保存的功能 441 This function a wrapper of GetFile and gives ability to save file.
439 442
440 保存的代码例子如下: 443 This is an example to save file that is uploaded:
441 444
442 func (this *MainController) Post() { 445 func (this *MainController) Post() {
443 this.SaveToFile("the_file","/var/www/uploads/uploaded_file.txt"") 446 this.SaveToFile("the_file","/var/www/uploads/uploaded_file.txt"")
444 } 447 }
445 448
446 ### JSON和XML输出 449 ###Output Json and XML
447 beego当初设计的时候就考虑了API功能的设计,而我们在设计API的时候经常是输出JSON或者XML数据,那么beego提供了这样的方式直接输出: 450 Beego considered API function design at the beginning, and we often use Json or XML format data as output. Therefore, it's no reason that Beego doesn't support it:
448 451
449 JSON数据直接输出,设置`content-type``application/json` 452 Set `content-type` to `application/json` for output raw Json format data:
450 453
451 func (this *AddController) Get() { 454 func (this *AddController) Get() {
452 mystruct := { ... } 455 mystruct := { ... }
...@@ -454,7 +457,7 @@ JSONæ•°æ®ç›´æŽ¥è¾“出,设置`content-type`为`application/json`: ...@@ -454,7 +457,7 @@ JSONæ•°æ®ç›´æŽ¥è¾“出,设置`content-type`为`application/json`:
454 this.ServeJson() 457 this.ServeJson()
455 } 458 }
456 459
457 XML数据直接输出,设置`content-type``application/xml` 460 Set `content-type` to `application/xml` for output raw XML format data:
458 461
459 func (this *AddController) Get() { 462 func (this *AddController) Get() {
460 mystruct := { ... } 463 mystruct := { ... }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!