Adds a new StringHTML Render method

This new `StringHTML()` method will allow users to render HTML without
the need of an actual file on disk.

Example:
```
r.GET("/saludos", func(c *gin.Context) {
	c.StringHTML(200, "<html><body>Hola amigo!</body></html>")
})
```
Signed-off-by: Salim Afiune <salim@afiunemaya.com.mx>
This commit is contained in:
Salim Afiune 2017-07-27 15:51:56 -04:00
parent 7fafb3f4a1
commit 820b43916a
4 changed files with 54 additions and 3 deletions

View File

@ -665,6 +665,11 @@ func (c *Context) YAML(code int, obj interface{}) {
c.Render(code, render.YAML{Data: obj}) c.Render(code, render.YAML{Data: obj})
} }
// StringHTML renders the provided content by setting the Content-Type as "text/html".
func (c *Context) StringHTML(code int, content string, values ...interface{}) {
c.Render(code, render.StringHTML{Format: content, Data: values})
}
// String writes the given string into the response body. // String writes the given string into the response body.
func (c *Context) String(code int, format string, values ...interface{}) { func (c *Context) String(code int, format string, values ...interface{}) {
c.Render(code, render.String{Format: format, Data: values}) c.Render(code, render.String{Format: format, Data: values})

View File

@ -733,6 +733,19 @@ func TestContextRenderString(t *testing.T) {
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8") assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
} }
// TestContextStringHTML tests that the response is returned
// with Content-Type set to text/html
func TestContextRenderStringHTML(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.StringHTML(201, "<html><body><h1>test %s %d</h1></body><html>", "string", 2)
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "<html><body><h1>test string 2</h1></body><html>")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
}
// Tests that no String is rendered if code is 204 // Tests that no String is rendered if code is 204
func TestContextRenderNoContentString(t *testing.T) { func TestContextRenderNoContentString(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -161,6 +161,19 @@ func TestRenderString(t *testing.T) {
assert.Equal(t, w.Header().Get("Content-Type"), "text/plain; charset=utf-8") assert.Equal(t, w.Header().Get("Content-Type"), "text/plain; charset=utf-8")
} }
func TestRenderStringHTML(t *testing.T) {
w := httptest.NewRecorder()
err := (StringHTML{
Format: "<html><body><h1>Hola mi %s numero %d</h1></body></html>",
Data: []interface{}{"amigo", 1},
}).Render(w)
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "<html><body><h1>Hola mi amigo numero 1</h1></body></html>")
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
}
func TestRenderHTMLTemplate(t *testing.T) { func TestRenderHTMLTemplate(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`)) templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))

View File

@ -18,7 +18,7 @@ type String struct {
var plainContentType = []string{"text/plain; charset=utf-8"} var plainContentType = []string{"text/plain; charset=utf-8"}
func (r String) Render(w http.ResponseWriter) error { func (r String) Render(w http.ResponseWriter) error {
WriteString(w, r.Format, r.Data) WriteString(w, r.Format, r.Data, false)
return nil return nil
} }
@ -26,11 +26,31 @@ func (r String) WriteContentType(w http.ResponseWriter) {
writeContentType(w, plainContentType) writeContentType(w, plainContentType)
} }
func WriteString(w http.ResponseWriter, format string, data []interface{}) { func WriteString(w http.ResponseWriter, format string, data []interface{}, html bool) {
writeContentType(w, plainContentType) if html {
writeContentType(w, htmlContentType)
} else {
writeContentType(w, plainContentType)
}
if len(data) > 0 { if len(data) > 0 {
fmt.Fprintf(w, format, data...) fmt.Fprintf(w, format, data...)
} else { } else {
io.WriteString(w, format) io.WriteString(w, format)
} }
} }
// StringHTML will function exactly the same as the String struct
// but it will inject an html ContentType to the response
type StringHTML struct {
Format string
Data []interface{}
}
func (r StringHTML) Render(w http.ResponseWriter) error {
WriteString(w, r.Format, r.Data, true)
return nil
}
func (r StringHTML) WriteContentType(w http.ResponseWriter) {
writeContentType(w, htmlContentType)
}