github.com/res-am/buffalo@v0.11.1/render/string.go (about)

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