github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/func.go (about)

     1  package render
     2  
     3  import "io"
     4  
     5  // RendererFunc is the interface for the the function
     6  // needed by the Func renderer.
     7  type RendererFunc func(io.Writer, Data) error
     8  
     9  type funcRenderer struct {
    10  	contentType string
    11  	renderFunc  RendererFunc
    12  }
    13  
    14  // ContentType returns the content type for this render.
    15  // Examples would be "text/html" or "application/json".
    16  func (s funcRenderer) ContentType() string {
    17  	return s.contentType
    18  }
    19  
    20  // Render the provided Data to the provider Writer using the
    21  // RendererFunc provide.
    22  func (s funcRenderer) Render(w io.Writer, data Data) error {
    23  	return s.renderFunc(w, data)
    24  }
    25  
    26  // Func renderer allows for easily building one of renderers
    27  // using just a RendererFunc and not having to build a whole
    28  // implementation of the Render interface.
    29  func Func(s string, fn RendererFunc) Renderer {
    30  	return funcRenderer{
    31  		contentType: s,
    32  		renderFunc:  fn,
    33  	}
    34  }
    35  
    36  // Func renderer allows for easily building one of renderers
    37  // using just a RendererFunc and not having to build a whole
    38  // implementation of the Render interface.
    39  func (e *Engine) Func(s string, fn RendererFunc) Renderer {
    40  	return Func(s, fn)
    41  }