2015-05-22 17:21:23 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-05-07 10:44:52 +00:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-30 12:45:13 +00:00
|
|
|
"io"
|
2015-05-07 10:44:52 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// String contains the given interface object slice and its format.
|
2015-05-18 13:45:24 +00:00
|
|
|
type String struct {
|
|
|
|
Format string
|
|
|
|
Data []interface{}
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 14:39:25 +00:00
|
|
|
var plainContentType = []string{"text/plain; charset=utf-8"}
|
2015-05-22 02:44:29 +00:00
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// Render (String) writes data with custom ContentType.
|
2015-06-04 03:25:21 +00:00
|
|
|
func (r String) Render(w http.ResponseWriter) error {
|
2015-06-04 10:53:42 +00:00
|
|
|
WriteString(w, r.Format, r.Data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// WriteContentType (String) writes Plain ContentType.
|
2017-01-09 15:24:48 +00:00
|
|
|
func (r String) WriteContentType(w http.ResponseWriter) {
|
2015-06-13 02:29:10 +00:00
|
|
|
writeContentType(w, plainContentType)
|
2017-01-09 15:24:48 +00:00
|
|
|
}
|
2015-06-13 02:29:10 +00:00
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// WriteString writes data according to its format and write custom ContentType.
|
2017-01-09 15:24:48 +00:00
|
|
|
func WriteString(w http.ResponseWriter, format string, data []interface{}) {
|
|
|
|
writeContentType(w, plainContentType)
|
2015-06-04 10:53:42 +00:00
|
|
|
if len(data) > 0 {
|
|
|
|
fmt.Fprintf(w, format, data...)
|
2018-08-19 14:52:43 +00:00
|
|
|
return
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|
2018-08-19 14:52:43 +00:00
|
|
|
io.WriteString(w, format)
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|