From 4a3902432a9559fa0c4927bce8b6971dbd77caeb Mon Sep 17 00:00:00 2001 From: Ricky Lin Date: Tue, 24 Dec 2013 00:13:24 +0800 Subject: [PATCH 1/2] Added LayoutSections property to Controller users can define 0 or multipe sections in the Layout template file so all kinds of content such as scripts, css can go to proper sections in the generated html file. --- controller.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/controller.go b/controller.go index 84c8cd74..1f148f06 100644 --- a/controller.go +++ b/controller.go @@ -38,6 +38,7 @@ type Controller struct { actionName string TplNames string Layout string + LayoutSections map[string]string // the key is the section name and the value is the template name TplExt string _xsrf_token string gotofunc string @@ -161,6 +162,25 @@ func (c *Controller) RenderBytes() ([]byte, error) { } tplcontent, _ := ioutil.ReadAll(newbytes) c.Data["LayoutContent"] = template.HTML(string(tplcontent)) + + if c.LayoutSections != nil { + for sectionName, sectionTpl := range c.LayoutSections { + if (sectionTpl == "") { + c.Data[sectionName] = "" + continue + } + + sectionBytes := bytes.NewBufferString("") + err = BeeTemplates[sectionTpl].ExecuteTemplate(sectionBytes, sectionTpl, c.Data) + if err != nil { + Trace("template Execute err:", err) + return nil, err + } + sectionContent, _ := ioutil.ReadAll(sectionBytes) + c.Data[sectionName] = template.HTML(string(sectionContent)) + } + } + ibytes := bytes.NewBufferString("") err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data) if err != nil { From 0b659961baf8e603fba571fea82fb1b5f43bb938 Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 24 Dec 2013 15:27:00 +0800 Subject: [PATCH 2/2] clearly the router, If user set the third params, will not follow the RESTful method --- router.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/router.go b/router.go index dd2567c0..72211c29 100644 --- a/router.go +++ b/router.go @@ -566,10 +566,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) for _, route := range p.fixrouters { n := len(requestPath) if requestPath == route.pattern { - runrouter = route.controllerType - findrouter = true runMethod = p.getRunMethod(r.Method, context, route) - break + if runMethod != "" { + runrouter = route.controllerType + findrouter = true + break + } } // pattern /admin url /admin 200 /admin/ 404 // pattern /admin/ url /admin 301 /admin/ 200 @@ -608,11 +610,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) //reassemble query params and add to RawQuery r.URL.RawQuery = url.Values(values).Encode() } - runrouter = route.controllerType - findrouter = true - context.Input.Params = params runMethod = p.getRunMethod(r.Method, context, route) - break + if runMethod != "" { + runrouter = route.controllerType + context.Input.Params = params + findrouter = true + break + } } } @@ -798,7 +802,7 @@ func (p *ControllerRegistor) getRunMethod(method string, context *beecontext.Con } else if m, ok = router.methods["*"]; ok { return m } else { - return strings.Title(method) + return "" } } else { return strings.Title(method)