mirror of https://github.com/gin-gonic/gin.git
Cosmetic changes in render
This commit is contained in:
parent
71bd9f4500
commit
306da81aaf
|
@ -26,6 +26,8 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const htmlContentType = "text/html; charset=utf-8"
|
||||||
|
|
||||||
func (r HTMLProduction) Instance(name string, data interface{}) Render {
|
func (r HTMLProduction) Instance(name string, data interface{}) Render {
|
||||||
return HTML{
|
return HTML{
|
||||||
Template: r.Template,
|
Template: r.Template,
|
||||||
|
@ -52,6 +54,6 @@ func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r HTML) Write(w http.ResponseWriter) error {
|
func (r HTML) Write(w http.ResponseWriter) error {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", htmlContentType)
|
||||||
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
|
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,13 +15,15 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const jsonContentType = "application/json; charset=utf-8"
|
||||||
|
|
||||||
func (r JSON) Write(w http.ResponseWriter) error {
|
func (r JSON) Write(w http.ResponseWriter) error {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", jsonContentType)
|
||||||
return json.NewEncoder(w).Encode(r.Data)
|
return json.NewEncoder(w).Encode(r.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r IndentedJSON) Write(w http.ResponseWriter) error {
|
func (r IndentedJSON) Write(w http.ResponseWriter) error {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", jsonContentType)
|
||||||
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -10,10 +10,12 @@ type String struct {
|
||||||
Data []interface{}
|
Data []interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const plainContentType = "text/plain; charset=utf-8"
|
||||||
|
|
||||||
func (r String) Write(w http.ResponseWriter) error {
|
func (r String) Write(w http.ResponseWriter) error {
|
||||||
header := w.Header()
|
header := w.Header()
|
||||||
if _, exist := header["Content-Type"]; !exist {
|
if _, exist := header["Content-Type"]; !exist {
|
||||||
header.Set("Content-Type", "text/plain; charset=utf-8")
|
header.Set("Content-Type", plainContentType)
|
||||||
}
|
}
|
||||||
if len(r.Data) > 0 {
|
if len(r.Data) > 0 {
|
||||||
fmt.Fprintf(w, r.Format, r.Data...)
|
fmt.Fprintf(w, r.Format, r.Data...)
|
||||||
|
|
|
@ -9,7 +9,9 @@ type XML struct {
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const xmlContentType = "application/xml; charset=utf-8"
|
||||||
|
|
||||||
func (r XML) Write(w http.ResponseWriter) error {
|
func (r XML) Write(w http.ResponseWriter) error {
|
||||||
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
w.Header().Set("Content-Type", xmlContentType)
|
||||||
return xml.NewEncoder(w).Encode(r.Data)
|
return xml.NewEncoder(w).Encode(r.Data)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue