github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/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  	te, ok := s.TemplateEngines["text"]
    19  	if !ok {
    20  		return fmt.Errorf("could not find a template engine for text")
    21  	}
    22  	t, err := te(s.body, data, s.Helpers)
    23  	if err != nil {
    24  		return err
    25  	}
    26  	_, err = w.Write([]byte(t))
    27  	return err
    28  }
    29  
    30  // String renderer that will run the string through
    31  // the github.com/gobuffalo/plush package and return
    32  // "text/plain" as the content type.
    33  func String(s string, args ...interface{}) Renderer {
    34  	e := New(Options{})
    35  	return e.String(s, args...)
    36  }
    37  
    38  // String renderer that will run the string through
    39  // the github.com/gobuffalo/plush package and return
    40  // "text/plain" as the content type.
    41  func (e *Engine) String(s string, args ...interface{}) Renderer {
    42  	if len(args) > 0 {
    43  		s = fmt.Sprintf(s, args...)
    44  	}
    45  	return stringRenderer{
    46  		Engine: e,
    47  		body:   s,
    48  	}
    49  }