Use a buffer in Render

This commit is contained in:
Nise Void 2018-11-30 14:48:29 +01:00
parent 890ff550eb
commit 717c6f65fc
Signed by untrusted user: NiseVoid
GPG Key ID: FBA14AC83EA602F3

View File

@ -1,7 +1,9 @@
package router package router
import ( import (
"bytes"
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
@ -76,9 +78,16 @@ func (c *Context) Render(code int, template string, data interface{}) error {
panic(`Cannot call render without a renderer set`) panic(`Cannot call render without a renderer set`)
} }
var b bytes.Buffer
err := c.router.Renderer.Render(&b, template, data, c)
if err != nil {
return err
}
c.Response.Header().Set(`Content-Type`, `text/html`) c.Response.Header().Set(`Content-Type`, `text/html`)
c.Response.WriteHeader(code) c.Response.WriteHeader(code)
return c.router.Renderer.Render(c.Response, template, data, c) _, _ = io.Copy(c.Response, &b)
return nil
} }
// Set sets a value in the context. Set is not safe to be used concurrently // Set sets a value in the context. Set is not safe to be used concurrently