Documentation ¶
Overview ¶
Package response is a helper for the core (https://godoc.org/github.com/volatile/core). It provides syntactic sugar that lets you easily write responses on the context.
Templates ¶
Templates are recursively parsed from the "templates" directory, just before running the server. Filenames (including extensions) and directory organization doesn't matter: all the files are parsed.
Global functions are predefined:
html uses a raw string without escaping it nl2br replaces new lines by "<br>"
TemplatesFuncs sets custom templates functions:
response.TemplatesFuncs(response.FuncMap{ "toUpper": strings.ToUpper, "toLower": strings.ToLower, })
TemplatesData sets custom templates functions:
response.TemplatesData(response.DataMap{ "toUpper": strings.ToUpper, "toLower": strings.ToLower, })
Template responds with a template:
response.Template(c, "hello", response.DataMap{ "foo": "bar", })
The context is always part of the data under key "c".
Index ¶
- func Bytes(c *core.Context, b []byte)
- func BytesStatus(c *core.Context, code int, b []byte)
- func ExecuteTemplate(wr io.Writer, c *core.Context, name string, data DataMap) error
- func JSON(c *core.Context, v interface{})
- func JSONStatus(c *core.Context, code int, v interface{})
- func Redirect(c *core.Context, urlStr string, code int)
- func Status(c *core.Context, code int)
- func String(c *core.Context, s string)
- func StringStatus(c *core.Context, code int, s string)
- func Template(c *core.Context, name string, data DataMap)
- func TemplateStatus(c *core.Context, code int, name string, data DataMap)
- func TemplatesData(data DataMap)
- func TemplatesFuncs(funcs FuncMap)
- type DataMap
- type FuncMap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesStatus ¶
BytesStatus responds with the status code and the slice of bytes b.
func ExecuteTemplate ¶
ExecuteTemplate works like the standard html/template.Template.ExecuteTemplate function. It always adds the following data to the map, but without overwriding the provided data:
c the current core.Context production the core.Production value
func JSONStatus ¶
JSONStatus responds with the status code and the JSON marshalled v.
func Redirect ¶
Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
func StringStatus ¶
StringStatus responds with the status code and the string s.
func TemplateStatus ¶
TemplateStatus responds with the status code and the template associated to name.
func TemplatesData ¶
func TemplatesData(data DataMap)
TemplatesData adds data that will be available to all templates. It is legal to overwrite elements of the map.
func TemplatesFuncs ¶
func TemplatesFuncs(funcs FuncMap)
TemplatesFuncs adds functions that will be available to all templates. It is legal to overwrite elements of the map.
Types ¶
type DataMap ¶
type DataMap map[string]interface{}
DataMap is the type of the map defining the mapping from names to data.
type FuncMap ¶
type FuncMap map[string]interface{}
FuncMap is the type of the map defining the mapping from names to functions. Each function must have either a single return value, or two return values of which the second has type error. In that case, if the second (error) argument evaluates to non-nil during execution, execution terminates and Execute returns that error. FuncMap has the same base type as FuncMap in "text/template", copied here so clients need not import "text/template".