render

package
v2.0.12+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSON = Generic(func(w io.Writer, data interface{}) error {
	enc := json.NewEncoder(w)
	enc.SetIndent("", "  ")
	return enc.Encode(data)
}, "application/json;charset=utf-8")

JSON serializes result object into JSON format

View Source
var TextXML = Generic(func(w io.Writer, data interface{}) error {
	return xml.NewEncoder(w).Encode(data)
}, "text/xml;charset=utf-8")

TextXML is the same as XML, but with "text/xml" content type

View Source
var XML = Generic(func(w io.Writer, data interface{}) error {
	return xml.NewEncoder(w).Encode(data)
}, "application/xml;charset=utf-8")

XML serializes result object into "application/xml" content type. Use TextXML for "text/xml" output.

Functions

func ContentType

func ContentType(tpl *template.Template) noodle.Middleware

ContentType creates renderer middleware that renders response to JSON, XML or HTML template based on Accept header. If Accept header is not specified, JSON is used as the output format. If nil is passed as template, only XML and JSON are rendered and text/html is output as JSON inside PRE tag.

func Generic

func Generic(s SerializerFunc, contentType string) noodle.Middleware

Generic factory for a middleware that lifts handler's data object from context and serializes it into HTTP ResponseWriter. Receives SerializerFunc and content type

func Template

func Template(tpl *template.Template) noodle.Middleware

Template creates middleware that applies pre-compiled template to handler's data object

func Yield

func Yield(r *http.Request, code int, data interface{})

Yield puts arbitrary data into context for subsequent rendering into response. The first argument of Yield is a HTTP status code.

Types

type SerializerFunc

type SerializerFunc func(io.Writer, interface{}) error

SerializerFunc is modelled after template's Execute method. Used by Generic middleware factory to create specific rendering middlewares

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL