forked from mirror/gin
Add HTML no template string output support #197
This commit is contained in:
parent
bee03fa7b0
commit
cf8150ed2b
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue