1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 12:30:54 +00:00

add RenderBytes function

This commit is contained in:
astaxie 2013-03-14 21:17:37 +08:00
parent 281747ab43
commit 31f505bfc1

View File

@ -83,6 +83,20 @@ func (c *Controller) Options() {
} }
func (c *Controller) Render() error { func (c *Controller) Render() error {
rb, err := c.RenderBytes()
if err != nil {
return err
} else {
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true)
c.Ctx.ContentType("text/html")
c.Ctx.ResponseWriter.Write(rb)
return nil
}
return nil
}
func (c *Controller) RenderBytes() ([]byte, error) {
//if the controller has set layout, then first get the tplname's content set the content to the layout //if the controller has set layout, then first get the tplname's content set the content to the layout
if c.Layout != "" { if c.Layout != "" {
if c.TplNames == "" { if c.TplNames == "" {
@ -95,22 +109,28 @@ func (c *Controller) Render() error {
tplcontent, _ := ioutil.ReadAll(newbytes) tplcontent, _ := ioutil.ReadAll(newbytes)
c.Data["LayoutContent"] = template.HTML(string(tplcontent)) c.Data["LayoutContent"] = template.HTML(string(tplcontent))
_, file = path.Split(c.Layout) _, file = path.Split(c.Layout)
err := BeeTemplates[subdir].ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) ibytes := bytes.NewBufferString("")
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil { if err != nil {
Trace("template Execute err:", err) Trace("template Execute err:", err)
} }
icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil
} else { } else {
if c.TplNames == "" { if c.TplNames == "" {
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
} }
_, file := path.Split(c.TplNames) _, file := path.Split(c.TplNames)
subdir := path.Dir(c.TplNames) subdir := path.Dir(c.TplNames)
err := BeeTemplates[subdir].ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) ibytes := bytes.NewBufferString("")
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil { if err != nil {
Trace("template Execute err:", err) Trace("template Execute err:", err)
} }
icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil
} }
return nil return []byte{}, nil
} }
func (c *Controller) Redirect(url string, code int) { func (c *Controller) Redirect(url string, code int) {