Skip to content

Commit 1f0bae3

Browse files
authored
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 <[email protected]>
1 parent f10daac commit 1f0bae3

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

context.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ type (
106106
// HTML sends an HTTP response with status code.
107107
HTML(int, string) error
108108

109+
// HTMLBlob sends an HTTP blob response with status code.
110+
HTMLBlob(int, []byte) error
111+
109112
// String sends a string response with status code.
110113
String(int, string) error
111114

@@ -349,24 +352,19 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
349352
if err = c.echo.Renderer.Render(buf, name, data, c); err != nil {
350353
return
351354
}
352-
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
353-
c.response.WriteHeader(code)
354-
_, err = c.response.Write(buf.Bytes())
355-
return
355+
return c.HTMLBlob(code, buf.Bytes())
356356
}
357357

358358
func (c *context) HTML(code int, html string) (err error) {
359-
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
360-
c.response.WriteHeader(code)
361-
_, err = c.response.Write([]byte(html))
362-
return
359+
return c.HTMLBlob(code, []byte(html))
360+
}
361+
362+
func (c *context) HTMLBlob(code int, b []byte) (err error) {
363+
return c.Blob(code, MIMETextHTMLCharsetUTF8, b)
363364
}
364365

365366
func (c *context) String(code int, s string) (err error) {
366-
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
367-
c.response.WriteHeader(code)
368-
_, err = c.response.Write([]byte(s))
369-
return
367+
return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(s))
370368
}
371369

372370
func (c *context) JSON(code int, i interface{}) (err error) {

website/content/recipes/twitter.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
+++
2-
title = "Twitter API Example"
3-
description = "Twitter API example for Echo"
2+
title = "Twitter Like API Example"
3+
description = "Twitter Like API example for Echo"
44
[menu.main]
55
name = "Twitter"
66
parent = "recipes"

0 commit comments

Comments
 (0)