From 1f0bae394adef28e7476df97ce566bf8024010e9 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Tue, 6 Dec 2016 18:57:06 -0800 Subject: [PATCH] Gannett digital master allow html buffers (#765) * Since some templating engines final result is a buffer, we've been casting them to strings, which is a bit unnecessary considering under the hood Echo supports this. * Since some templating engines final result is a buffer, we've been casting them to strings, which is a bit unnecessary considering under the hood Echo supports this. * Update context.go * Update context_test.go * Update context_test.go * Utilize same design pattern for HTMLBlob as JSONBlob * Streamlined a few more methods * more consistent Signed-off-by: Vishal Rana --- context.go | 22 ++++++++++------------ website/content/recipes/twitter.md | 4 ++-- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/context.go b/context.go index 71bc3e4f..61e112e7 100644 --- a/context.go +++ b/context.go @@ -106,6 +106,9 @@ type ( // HTML sends an HTTP response with status code. HTML(int, string) error + // HTMLBlob sends an HTTP blob response with status code. + HTMLBlob(int, []byte) error + // String sends a string response with status code. String(int, string) error @@ -349,24 +352,19 @@ func (c *context) Render(code int, name string, data interface{}) (err error) { if err = c.echo.Renderer.Render(buf, name, data, c); err != nil { return } - c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8) - c.response.WriteHeader(code) - _, err = c.response.Write(buf.Bytes()) - return + return c.HTMLBlob(code, buf.Bytes()) } func (c *context) HTML(code int, html string) (err error) { - c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8) - c.response.WriteHeader(code) - _, err = c.response.Write([]byte(html)) - return + return c.HTMLBlob(code, []byte(html)) +} + +func (c *context) HTMLBlob(code int, b []byte) (err error) { + return c.Blob(code, MIMETextHTMLCharsetUTF8, b) } func (c *context) String(code int, s string) (err error) { - c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8) - c.response.WriteHeader(code) - _, err = c.response.Write([]byte(s)) - return + return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(s)) } func (c *context) JSON(code int, i interface{}) (err error) { diff --git a/website/content/recipes/twitter.md b/website/content/recipes/twitter.md index 2ead0e19..c186c18a 100644 --- a/website/content/recipes/twitter.md +++ b/website/content/recipes/twitter.md @@ -1,6 +1,6 @@ +++ -title = "Twitter API Example" -description = "Twitter API example for Echo" +title = "Twitter Like API Example" +description = "Twitter Like API example for Echo" [menu.main] name = "Twitter" parent = "recipes"