From a7791c278669a5645276b246525fbf69b2dd8e34 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 19 Dec 2012 16:20:55 +0800 Subject: [PATCH] #2 #2 --- README.md | 14 +-- controller.go | 306 +++++++++++++++++++++++++------------------------- 2 files changed, 160 insertions(+), 160 deletions(-) diff --git a/README.md b/README.md index dbe9f849..e2a6a0ad 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Here is the canonical "Hello, world" example app for beego: } func (this *MainController) Get() { - this.Ct.WriteString("hello world") + this.Ctx.WriteString("hello world") } func main() { @@ -177,10 +177,10 @@ So you can define ChildStruct method to accomplish the interface's method, now l func (this *AddController) Post() { //data deal with - this.Ct.Request.ParseForm() - pkgname := this.Ct.Request.Form.Get("pkgname") - content := this.Ct.Request.Form.Get("content") - beego.Info(this.Ct.Request.Form) + this.Ctx.Request.ParseForm() + pkgname := this.Ctx.Request.Form.Get("pkgname") + content := this.Ctx.Request.Form.Get("content") + beego.Info(this.Ctx.Request.Form) pk := models.GetCruPkg(pkgname) if pk.Id == 0 { var pp models.PkgEntity @@ -194,7 +194,7 @@ So you can define ChildStruct method to accomplish the interface's method, now l at.Pkgid = pk.Id at.Content = content models.InsertArticle(at) - this.Ct.Redirect(302, "/admin/index") + this.Ctx.Redirect(302, "/admin/index") } ## View / Template @@ -216,7 +216,7 @@ 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 + c.TplNames = c.ChildName + "/" + c.Ctx.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` diff --git a/controller.go b/controller.go index 66b664db..1eae51eb 100644 --- a/controller.go +++ b/controller.go @@ -1,153 +1,153 @@ -package beego - -import ( - "bytes" - "encoding/json" - "encoding/xml" - "html/template" - "io/ioutil" - "net/http" - "net/url" - "path" - "strconv" -) - -type Controller struct { - Ct *Context - Tpl *template.Template - Data map[interface{}]interface{} - ChildName string - TplNames string - Layout string - TplExt string -} - -type ControllerInterface interface { - Init(ct *Context, cn string) - Prepare() - Get() - Post() - Delete() - Put() - Head() - Patch() - Options() - Finish() - Render() error -} - -func (c *Controller) Init(ct *Context, cn string) { - c.Data = make(map[interface{}]interface{}) - c.Tpl = template.New(cn + ct.Request.Method) - c.Tpl = c.Tpl.Funcs(beegoTplFuncMap) - c.Layout = "" - c.TplNames = "" - c.ChildName = cn - c.Ct = ct - c.TplExt = "tpl" - -} - -func (c *Controller) Prepare() { - -} - -func (c *Controller) Finish() { - -} - -func (c *Controller) Get() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Post() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Delete() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Put() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Head() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Patch() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Options() { - http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) -} - -func (c *Controller) Render() error { - //if the controller has set layout, then first get the tplname's content set the content to the layout - if c.Layout != "" { - if c.TplNames == "" { - c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt - } - t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout)) - if err != nil { - Trace("template ParseFiles err:", err) - } - _, file := path.Split(c.TplNames) - newbytes := bytes.NewBufferString("") - t.ExecuteTemplate(newbytes, file, c.Data) - tplcontent, _ := ioutil.ReadAll(newbytes) - c.Data["LayoutContent"] = template.HTML(string(tplcontent)) - _, file = path.Split(c.Layout) - err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data) - if err != nil { - Trace("template Execute err:", err) - } - } else { - if c.TplNames == "" { - c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt - } - t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames)) - if err != nil { - Trace("template ParseFiles err:", err) - } - _, file := path.Split(c.TplNames) - err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data) - if err != nil { - Trace("template Execute err:", err) - } - } - return nil -} - -func (c *Controller) Redirect(url string, code int) { - c.Ct.Redirect(code, url) -} - -func (c *Controller) ServeJson() { - content, err := json.MarshalIndent(c.Data, "", " ") - if err != nil { - http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError) - return - } - c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true) - c.Ct.ContentType("json") - c.Ct.ResponseWriter.Write(content) -} - -func (c *Controller) ServeXml() { - content, err := xml.Marshal(c.Data) - if err != nil { - http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError) - return - } - c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true) - c.Ct.ContentType("xml") - c.Ct.ResponseWriter.Write(content) -} - -func (c *Controller) Input() url.Values { - c.Ct.Request.ParseForm() - return c.Ct.Request.Form -} +package beego + +import ( + "bytes" + "encoding/json" + "encoding/xml" + "html/template" + "io/ioutil" + "net/http" + "net/url" + "path" + "strconv" +) + +type Controller struct { + Ctx *Context + Tpl *template.Template + Data map[interface{}]interface{} + ChildName string + TplNames string + Layout string + TplExt string +} + +type ControllerInterface interface { + Init(ct *Context, cn string) + Prepare() + Get() + Post() + Delete() + Put() + Head() + Patch() + Options() + Finish() + Render() error +} + +func (c *Controller) Init(ctx *Context, cn string) { + c.Data = make(map[interface{}]interface{}) + c.Tpl = template.New(cn + ctx.Request.Method) + c.Tpl = c.Tpl.Funcs(beegoTplFuncMap) + c.Layout = "" + c.TplNames = "" + c.ChildName = cn + c.Ctx = ctx + c.TplExt = "tpl" + +} + +func (c *Controller) Prepare() { + +} + +func (c *Controller) Finish() { + +} + +func (c *Controller) Get() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Post() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Delete() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Put() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Head() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Patch() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Options() { + http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) +} + +func (c *Controller) Render() error { + //if the controller has set layout, then first get the tplname's content set the content to the layout + if c.Layout != "" { + if c.TplNames == "" { + c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt + } + t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout)) + if err != nil { + Trace("template ParseFiles err:", err) + } + _, file := path.Split(c.TplNames) + newbytes := bytes.NewBufferString("") + t.ExecuteTemplate(newbytes, file, c.Data) + tplcontent, _ := ioutil.ReadAll(newbytes) + c.Data["LayoutContent"] = template.HTML(string(tplcontent)) + _, file = path.Split(c.Layout) + err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) + if err != nil { + Trace("template Execute err:", err) + } + } else { + if c.TplNames == "" { + c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt + } + t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames)) + if err != nil { + Trace("template ParseFiles err:", err) + } + _, file := path.Split(c.TplNames) + err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) + if err != nil { + Trace("template Execute err:", err) + } + } + return nil +} + +func (c *Controller) Redirect(url string, code int) { + c.Ctx.Redirect(code, url) +} + +func (c *Controller) ServeJson() { + content, err := json.MarshalIndent(c.Data, "", " ") + if err != nil { + http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) + return + } + c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true) + c.Ctx.ContentType("json") + c.Ctx.ResponseWriter.Write(content) +} + +func (c *Controller) ServeXml() { + content, err := xml.Marshal(c.Data) + if err != nil { + http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) + return + } + c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true) + c.Ctx.ContentType("xml") + c.Ctx.ResponseWriter.Write(content) +} + +func (c *Controller) Input() url.Values { + c.Ctx.Request.ParseForm() + return c.Ctx.Request.Form +}