text.go 1018 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package render
  5. import (
  6. "fmt"
  7. "io"
  8. "net/http"
  9. )
  10. // String contains the given interface object slice and its format.
  11. type String struct {
  12. Format string
  13. Data []interface{}
  14. }
  15. var plainContentType = []string{"text/plain; charset=utf-8"}
  16. // Render (String) writes data with custom ContentType.
  17. func (r String) Render(w http.ResponseWriter) error {
  18. WriteString(w, r.Format, r.Data)
  19. return nil
  20. }
  21. // WriteContentType (String) writes Plain ContentType.
  22. func (r String) WriteContentType(w http.ResponseWriter) {
  23. writeContentType(w, plainContentType)
  24. }
  25. // WriteString writes data according to its format and write custom ContentType.
  26. func WriteString(w http.ResponseWriter, format string, data []interface{}) {
  27. writeContentType(w, plainContentType)
  28. if len(data) > 0 {
  29. fmt.Fprintf(w, format, data...)
  30. return
  31. }
  32. io.WriteString(w, format)
  33. }