From cf8150ed2bb0f4c503cddc1737731b5d28798b82 Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Sun, 8 Mar 2015 17:50:58 +0100 Subject: [PATCH] Add HTML no template string output support #197 --- context.go | 5 +++++ render/render.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/context.go b/context.go index d8775142..5d7e02a9 100644 --- a/context.go +++ b/context.go @@ -351,6 +351,11 @@ func (c *Context) String(code int, format string, values ...interface{}) { c.Render(code, render.Plain, format, values) } +// Writes the given string into the response body and sets the Content-Type to "text/html" without template. +func (c *Context) HTMLString(code int, format string, values ...interface{}) { + c.Render(code, render.HTMLPlain, format, values) +} + // Returns a HTTP redirect to the specific location. func (c *Context) Redirect(code int, location string) { if code >= 300 && code <= 308 { diff --git a/render/render.go b/render/render.go index 467a3299..bc7bceb8 100644 --- a/render/render.go +++ b/render/render.go @@ -26,6 +26,9 @@ type ( // Plain text plainRender struct{} + // HTML Plain text + htmlPlainRender struct{} + // Redirects redirectRender struct{} @@ -45,6 +48,7 @@ var ( JSON = jsonRender{} XML = xmlRender{} Plain = plainRender{} + HTMLPlain = htmlPlainRender{} Redirect = redirectRender{} HTMLDebug = &htmlDebugRender{} ) @@ -85,6 +89,19 @@ func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{} return err } +func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { + writeHeader(w, code, "text/html") + format := data[0].(string) + args := data[1].([]interface{}) + var err error + if len(args) > 0 { + _, err = w.Write([]byte(fmt.Sprintf(format, args...))) + } else { + _, err = w.Write([]byte(format)) + } + return err +} + func (r *htmlDebugRender) AddGlob(pattern string) { r.globs = append(r.globs, pattern) }