31f505bf by astaxie

add RenderBytes function

1 parent 281747ab
...@@ -83,6 +83,20 @@ func (c *Controller) Options() { ...@@ -83,6 +83,20 @@ func (c *Controller) Options() {
83 } 83 }
84 84
85 func (c *Controller) Render() error { 85 func (c *Controller) Render() error {
86 rb, err := c.RenderBytes()
87
88 if err != nil {
89 return err
90 } else {
91 c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true)
92 c.Ctx.ContentType("text/html")
93 c.Ctx.ResponseWriter.Write(rb)
94 return nil
95 }
96 return nil
97 }
98
99 func (c *Controller) RenderBytes() ([]byte, error) {
86 //if the controller has set layout, then first get the tplname's content set the content to the layout 100 //if the controller has set layout, then first get the tplname's content set the content to the layout
87 if c.Layout != "" { 101 if c.Layout != "" {
88 if c.TplNames == "" { 102 if c.TplNames == "" {
...@@ -95,22 +109,28 @@ func (c *Controller) Render() error { ...@@ -95,22 +109,28 @@ func (c *Controller) Render() error {
95 tplcontent, _ := ioutil.ReadAll(newbytes) 109 tplcontent, _ := ioutil.ReadAll(newbytes)
96 c.Data["LayoutContent"] = template.HTML(string(tplcontent)) 110 c.Data["LayoutContent"] = template.HTML(string(tplcontent))
97 _, file = path.Split(c.Layout) 111 _, file = path.Split(c.Layout)
98 err := BeeTemplates[subdir].ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) 112 ibytes := bytes.NewBufferString("")
113 err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
99 if err != nil { 114 if err != nil {
100 Trace("template Execute err:", err) 115 Trace("template Execute err:", err)
101 } 116 }
117 icontent, _ := ioutil.ReadAll(ibytes)
118 return icontent, nil
102 } else { 119 } else {
103 if c.TplNames == "" { 120 if c.TplNames == "" {
104 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt 121 c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
105 } 122 }
106 _, file := path.Split(c.TplNames) 123 _, file := path.Split(c.TplNames)
107 subdir := path.Dir(c.TplNames) 124 subdir := path.Dir(c.TplNames)
108 err := BeeTemplates[subdir].ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) 125 ibytes := bytes.NewBufferString("")
126 err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
109 if err != nil { 127 if err != nil {
110 Trace("template Execute err:", err) 128 Trace("template Execute err:", err)
111 } 129 }
130 icontent, _ := ioutil.ReadAll(ibytes)
131 return icontent, nil
112 } 132 }
113 return nil 133 return []byte{}, nil
114 } 134 }
115 135
116 func (c *Controller) Redirect(url string, code int) { 136 func (c *Controller) Redirect(url string, code int) {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!