Documentation ¶
Overview ¶
Package render defines rendering functionality.
Index ¶
- type Renderer
- func (r *Renderer) AllowedResponseCode(code int) bool
- func (r *Renderer) HTML500(w http.ResponseWriter, err error)
- func (r *Renderer) JSON500(w http.ResponseWriter, err error)
- func (r *Renderer) RenderEmail(tmpl string, data interface{}) ([]byte, error)
- func (r *Renderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})
- func (r *Renderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{})
- func (r *Renderer) RenderJSON(w http.ResponseWriter, code int, data interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer is responsible for rendering various content and templates like HTML and JSON responses.
func (*Renderer) AllowedResponseCode ¶ added in v0.3.0
AllowedResponseCode returns true if the code is a permitted response code, false otherwise.
func (*Renderer) HTML500 ¶
func (r *Renderer) HTML500(w http.ResponseWriter, err error)
HTML500 renders the given error as HTML. In production mode, this always renders a generic "server error" message. In debug, it returns the actual error from the caller.
func (*Renderer) JSON500 ¶
func (r *Renderer) JSON500(w http.ResponseWriter, err error)
JSON500 renders the given error as JSON. In production mode, this always renders a generic "server error" message. In debug, it returns the actual error from the caller.
func (*Renderer) RenderEmail ¶ added in v0.12.0
RenderEmail renders the given email HTML template by name. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.
The buffers are fetched via a sync.Pool to reduce allocations and improve performance.
func (*Renderer) RenderHTML ¶
func (r *Renderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})
RenderHTML calls RenderHTMLStatus with a http.StatusOK (200).
func (*Renderer) RenderHTMLStatus ¶
func (r *Renderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{})
RenderHTMLStatus renders the given HTML template by name. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.
If template rendering fails, a generic 500 page is returned. In dev mode, the error is included on the page. If flushing the buffer to the response fails, an error is logged, but no recovery is attempted.
The buffers are fetched via a sync.Pool to reduce allocations and improve performance.
func (*Renderer) RenderJSON ¶
func (r *Renderer) RenderJSON(w http.ResponseWriter, code int, data interface{})
RenderJSON renders the interface as JSON. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.
If the provided data is nil and the response code is a 200, the result will be `{"ok":true}`. If the code is not a 200, the response will be of the format `{"error":"<val>"}` where val is the JSON-escaped http.StatusText for the provided code.
If rendering fails, a generic 500 JSON response is returned. In dev mode, the error is included in the payload. If flushing the buffer to the response fails, an error is logged, but no recovery is attempted.
The buffers are fetched via a sync.Pool to reduce allocations and improve performance.