gin/render/text.go
Salim Afiune 820b43916a 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>
2017-07-31 10:03:06 -04:00

57 lines
1.2 KiB
Go

// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package render
import (
"fmt"
"io"
"net/http"
)
type String struct {
Format string
Data []interface{}
}
var plainContentType = []string{"text/plain; charset=utf-8"}
func (r String) Render(w http.ResponseWriter) error {
WriteString(w, r.Format, r.Data, false)
return nil
}
func (r String) WriteContentType(w http.ResponseWriter) {
writeContentType(w, plainContentType)
}
func WriteString(w http.ResponseWriter, format string, data []interface{}, html bool) {
if html {
writeContentType(w, htmlContentType)
} else {
writeContentType(w, plainContentType)
}
if len(data) > 0 {
fmt.Fprintf(w, format, data...)
} else {
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)
}