diff --git a/context.go b/context.go index d877514..5d7e02a 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 467a329..bc7bceb 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) }