mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 10:30:55 +00:00
add RenderBytes function
This commit is contained in:
parent
281747ab43
commit
31f505bfc1
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user