1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 17:00:50 +00:00

add more intro

This commit is contained in:
xiemengjun 2012-12-16 23:43:42 +08:00
parent 1318f88e04
commit 53ce6ad3b9

159
README.md
View File

@ -100,9 +100,168 @@ also You can apply filters only when certain prefix URL path exist:
## Controller / Strcut
============
you type a ChildStruct has anonymous type `beego.Controller`
type xxxController struct {
beego.Controller
}
the `beego.Controller` is `beego.ControllerInterface` has the follow method:
- Init(ct *Context, cn string)
this function is init the Context, ChildStruct' name and the Controller's variables.
- Prepare()
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.
- Get()
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.
- Post()
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.
- Delete()
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.
- Put()
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.
- Head()
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.
- Patch()
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.
- Options()
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.
- Finish()
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.
- Render() error
this function is to render the template as user defined. In the strcut you need to call.
So you can define ChildStruct method to accomplish the interface's method, now let us see an example:
type AddController struct {
beego.Controller
}
func (this *AddController) Prepare() {
}
func (this *AddController) Get() {
this.Layout = "admin/layout.html"
this.TplNames = "admin/add.tpl"
}
func (this *AddController) Post() {
//数据处理
this.Ct.Request.ParseForm()
pkgname := this.Ct.Request.Form.Get("pkgname")
content := this.Ct.Request.Form.Get("content")
beego.Info(this.Ct.Request.Form)
pk := models.GetCruPkg(pkgname)
if pk.Id == 0 {
var pp models.PkgEntity
pp.Pid = 0
pp.Pathname = pkgname
pp.Intro = pkgname
models.InsertPkg(pp)
pk = models.GetCruPkg(pkgname)
}
var at models.Article
at.Pkgid = pk.Id
at.Content = content
models.InsertArticle(at)
this.Ct.Redirect(302, "/admin/index")
}
## View / Template
============
### template view path
the default viewPath is `/views`,you can put the template file in the views.beego will find the template from viewpath.
also you can modify the viewpaths like this:
beego.ViewsPath = "/myviewpath"
### template names
beego will find the template from viewpath. the file is set by user like
this.TplNames = "admin/add.tpl"
then beego will find the file in the path:`/views/admin/add.tpl`
if you don't set TplNames,beego will find like this:
c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt
So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl"
So beego will file the file in the path:`/view/AddController/POST.tpl`
### autoRender
In the controller you needn't to call render function. beego will auto call this function after HTTP' Method Call.
also you can close the autoRendder like this:
beego.AutoRender = false
### layout
beego also support layout. beego's layout is like this:
this.Layout = "admin/layout.html"
this.TplNames = "admin/add.tpl"
in the layout.html you must define the variable like this to show sub template's content:
{{.LayoutContent}}
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.
### template function
beego support users to define template function like this:
func hello(in string)(out string){
out = in + "world"
return
}
beego.AddFuncMap("hi",hello)
then in you template you can use it like this:
{{.Content | hi}}
beego has three default defined funtion:
- beegoTplFuncMap["markdown"] = MarkDown
MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown"
- beegoTplFuncMap["dateformat"] = DateFormat
DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat"
- beegoTplFuncMap["compare"] = Compare
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"
## Config
============