github.com/rotblauer/buffalo@v0.7.1-0.20170112214545-7aa55ef80dd3/render/string.go (about)

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