Add HTML no template string output support #197

This commit is contained in:
Javier Provecho Fernandez 2015-03-08 17:50:58 +01:00
parent bee03fa7b0
commit cf8150ed2b
2 changed files with 22 additions and 0 deletions

View File

@ -351,6 +351,11 @@ func (c *Context) String(code int, format string, values ...interface{}) {
c.Render(code, render.Plain, format, values) 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. // Returns a HTTP redirect to the specific location.
func (c *Context) Redirect(code int, location string) { func (c *Context) Redirect(code int, location string) {
if code >= 300 && code <= 308 { if code >= 300 && code <= 308 {

View File

@ -26,6 +26,9 @@ type (
// Plain text // Plain text
plainRender struct{} plainRender struct{}
// HTML Plain text
htmlPlainRender struct{}
// Redirects // Redirects
redirectRender struct{} redirectRender struct{}
@ -45,6 +48,7 @@ var (
JSON = jsonRender{} JSON = jsonRender{}
XML = xmlRender{} XML = xmlRender{}
Plain = plainRender{} Plain = plainRender{}
HTMLPlain = htmlPlainRender{}
Redirect = redirectRender{} Redirect = redirectRender{}
HTMLDebug = &htmlDebugRender{} HTMLDebug = &htmlDebugRender{}
) )
@ -85,6 +89,19 @@ func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{}
return err 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) { func (r *htmlDebugRender) AddGlob(pattern string) {
r.globs = append(r.globs, pattern) r.globs = append(r.globs, pattern)
} }