github.com/cheikhshift/buffalo@v0.9.5/render/string.go (about)

     1  package render
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  )
     7  
     8  type stringRenderer struct {
     9  	*Engine
    10  	body string
    11  }
    12  
    13  func (s stringRenderer) ContentType() string {
    14  	return "text/plain; charset=utf-8"
    15  }
    16  
    17  func (s stringRenderer) Render(w io.Writer, data Data) error {
    18  	t, err := s.TemplateEngine(s.body, data, s.Helpers)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	_, err = w.Write([]byte(t))
    23  	return err
    24  }
    25  
    26  // String renderer that will run the string through
    27  // the github.com/gobuffalo/plush package and return
    28  // "text/plain" as the content type.
    29  func String(s string, args ...interface{}) Renderer {
    30  	e := New(Options{})
    31  	return e.String(s, args...)
    32  }
    33  
    34  // String renderer that will run the string through
    35  // the github.com/gobuffalo/plush package and return
    36  // "text/plain" as the content type.
    37  func (e *Engine) String(s string, args ...interface{}) Renderer {
    38  	if len(args) > 0 {
    39  		s = fmt.Sprintf(s, args...)
    40  	}
    41  	return stringRenderer{
    42  		Engine: e,
    43  		body:   s,
    44  	}
    45  }