response

package module
v0.0.0-...-c83849d Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2016 License: MIT Imports: 11 Imported by: 2

README

response

Package response is a helper for the core.
It provides syntactic sugar that lets you easily write responses on the context.

GoDoc

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(c *core.Context, b []byte)

Bytes responds with the slice of bytes b.

func BytesStatus

func BytesStatus(c *core.Context, code int, b []byte)

BytesStatus responds with the status code and the slice of bytes b.

func ExecuteTemplate

func ExecuteTemplate(wr io.Writer, c *core.Context, name string, data DataMap) error

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 JSON

func JSON(c *core.Context, v interface{})

JSON responds with the JSON marshalled v.

func JSONStatus

func JSONStatus(c *core.Context, code int, v interface{})

JSONStatus responds with the status code and the JSON marshalled v.

func Redirect

func Redirect(c *core.Context, urlStr string, code int)

Redirect replies to the request with a redirect to url, which may be a path relative to the request path.

func Status

func Status(c *core.Context, code int)

Status responds with the status code.

func String

func String(c *core.Context, s string)

String responds with the string s.

func StringStatus

func StringStatus(c *core.Context, code int, s string)

StringStatus responds with the status code and the string s.

func Template

func Template(c *core.Context, name string, data DataMap)

Template responds with the template associated to name.

func TemplateStatus

func TemplateStatus(c *core.Context, code int, name string, data DataMap)

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".

Jump to

Keyboard shortcuts

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