Documentation ¶
Overview ¶
Package render is a package that provides functionality for easily rendering JSON, XML, binary data, and HTML templates.
package main import ( "encoding/xml" "net/http" "github.com/unrolled/render" // or "gopkg.in/unrolled/render.v1" ) type ExampleXml struct { XMLName xml.Name `xml:"example"` One string `xml:"one,attr"` Two string `xml:"two,attr"` } func main() { r := render.New() mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Welcome, visit sub pages now.")) }) mux.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) { r.Data(w, http.StatusOK, []byte("Some binary data here.")) }) mux.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) { r.JSON(w, http.StatusOK, map[string]string{"hello": "json"}) }) mux.HandleFunc("/jsonp", func(w http.ResponseWriter, req *http.Request) { r.JSONP(w, http.StatusOK, "callbackName", map[string]string{"hello": "jsonp"}) }) mux.HandleFunc("/xml", func(w http.ResponseWriter, req *http.Request) { r.XML(w, http.StatusOK, ExampleXml{One: "hello", Two: "xml"}) }) mux.HandleFunc("/html", func(w http.ResponseWriter, req *http.Request) { // Assumes you have a template in ./templates called "example.tmpl" // $ mkdir -p templates && echo "<h1>Hello HTML world.</h1>" > templates/example.tmpl r.HTML(w, http.StatusOK, "example", nil) }) http.ListenAndServe("0.0.0.0:3000", mux) }
Index ¶
- Constants
- type Data
- type Delims
- type Engine
- type HTML
- type HTMLOptions
- type Head
- type JSON
- type JSONP
- type Options
- type Render
- func (r *Render) Data(w http.ResponseWriter, status int, v []byte)
- func (r *Render) HTML(w http.ResponseWriter, status int, name string, binding interface{}, ...)
- func (r *Render) JSON(w http.ResponseWriter, status int, v interface{})
- func (r *Render) JSONP(w http.ResponseWriter, status int, callback string, v interface{})
- func (r *Render) Render(w http.ResponseWriter, e Engine, data interface{})
- func (r *Render) XML(w http.ResponseWriter, status int, v interface{})
- type XML
Constants ¶
const ( // ContentType header constant. ContentType = "Content-Type" // ContentLength header constant. ContentLength = "Content-Length" // ContentBinary header value for binary data. ContentBinary = "application/octet-stream" // ContentJSON header value for JSON data. ContentJSON = "application/json" // ContentJSONP header value for JSONP data. ContentJSONP = "application/javascript" // ContentHTML header value for HTML data. ContentHTML = "text/html" // ContentXHTML header value for XHTML data. ContentXHTML = "application/xhtml+xml" // ContentXML header value for XML data. ContentXML = "text/xml" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Delims ¶
type Delims struct { // Left delimiter, defaults to {{. Left string // Right delimiter, defaults to }}. Right string }
Delims represents a set of Left and Right delimiters for HTML template rendering.
type Engine ¶
type Engine interface {
Render(http.ResponseWriter, interface{}) error
}
Engine is the generic interface for all responses.
type HTMLOptions ¶
type HTMLOptions struct { // Layout template name. Overrides Options.Layout. Layout string }
HTMLOptions is a struct for overriding some rendering Options for specific HTML call.
type Options ¶
type Options struct { // Directory to load templates. Default is "templates". Directory string // Layout template name. Will not render a layout if blank (""). Defaults to blank (""). Layout string // Extensions to parse template files from. Defaults to [".tmpl"]. Extensions []string // Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Defaults to []. Funcs []template.FuncMap // Delims sets the action delimiters to the specified strings in the Delims struct. Delims Delims // Appends the given character set to the Content-Type header. Default is "UTF-8". Charset string // Outputs human readable JSON. IndentJSON bool // Outputs human readable XML. IndentXML bool // Prefixes the JSON output with the given bytes. PrefixJSON []byte // Prefixes the XML output with the given bytes. PrefixXML []byte // Allows changing of output to XHTML instead of HTML. Default is "text/html" HTMLContentType string // If IsDevelopment is set to true, this will recompile the templates on every request. Default if false. IsDevelopment bool }
Options is a struct for specifying configuration options for the render.Render object.
type Render ¶
type Render struct {
// contains filtered or unexported fields
}
Render is a service that provides functions for easily writing JSON, XML, binary data, and HTML templates out to a HTTP Response.
func (*Render) Data ¶
func (r *Render) Data(w http.ResponseWriter, status int, v []byte)
Data writes out the raw bytes as binary data.
func (*Render) HTML ¶
func (r *Render) HTML(w http.ResponseWriter, status int, name string, binding interface{}, htmlOpt ...HTMLOptions)
HTML builds up the response from the specified template and bindings.
func (*Render) JSON ¶
func (r *Render) JSON(w http.ResponseWriter, status int, v interface{})
JSON marshals the given interface object and writes the JSON response.
func (*Render) JSONP ¶
func (r *Render) JSONP(w http.ResponseWriter, status int, callback string, v interface{})
JSONP marshals the given interface object and writes the JSON response.