1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

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 <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-12-06 18:57:06 -08:00 committed by GitHub
parent f10daac5d6
commit 1f0bae394a
2 changed files with 12 additions and 14 deletions

View File

@ -106,6 +106,9 @@ type (
// HTML sends an HTTP response with status code. // HTML sends an HTTP response with status code.
HTML(int, string) error 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 sends a string response with status code.
String(int, string) error 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 { if err = c.echo.Renderer.Render(buf, name, data, c); err != nil {
return return
} }
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8) return c.HTMLBlob(code, buf.Bytes())
c.response.WriteHeader(code)
_, err = c.response.Write(buf.Bytes())
return
} }
func (c *context) HTML(code int, html string) (err error) { func (c *context) HTML(code int, html string) (err error) {
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8) return c.HTMLBlob(code, []byte(html))
c.response.WriteHeader(code) }
_, err = c.response.Write([]byte(html))
return 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) { func (c *context) String(code int, s string) (err error) {
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8) return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(s))
c.response.WriteHeader(code)
_, err = c.response.Write([]byte(s))
return
} }
func (c *context) JSON(code int, i interface{}) (err error) { func (c *context) JSON(code int, i interface{}) (err error) {

View File

@ -1,6 +1,6 @@
+++ +++
title = "Twitter API Example" title = "Twitter Like API Example"
description = "Twitter API example for Echo" description = "Twitter Like API example for Echo"
[menu.main] [menu.main]
name = "Twitter" name = "Twitter"
parent = "recipes" parent = "recipes"