1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Update docs with new API

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-07-16 23:03:45 -07:00
parent ace5843983
commit e550355a5f
2 changed files with 71 additions and 7 deletions

View File

@ -106,8 +106,8 @@ func (c *Context) Bind(i interface{}) error {
return c.echo.binder(c.request, i) return c.echo.binder(c.request, i)
} }
// Render invokes the registered HTML template renderer and sends a text/html // Render renders a template with data and sends a text/html response with status
// response with status code. // code. Templates can be registered using `Echo.SetRenderer()`.
func (c *Context) Render(code int, name string, data interface{}) (err error) { func (c *Context) Render(code int, name string, data interface{}) (err error) {
if c.echo.renderer == nil { if c.echo.renderer == nil {
return RendererNotRegistered return RendererNotRegistered

View File

@ -46,6 +46,10 @@ and message `HTTPError.Message`.
Enables debug mode. Enables debug mode.
### Disable colored log
`Echo.DisableColoredLog()`
## Routing ## Routing
Echo's router is [fast, optimized](https://github.com/labstack/echo#benchmark) and Echo's router is [fast, optimized](https://github.com/labstack/echo#benchmark) and
@ -299,30 +303,90 @@ $ curl -d "name=joe" http://localhost:1323/users
## Response ## Response
### Template
```go
Context.Render(code int, name string, data interface{}) error
```
Renders a template with data and sends a text/html response with status code. Templates
can be registered using `Echo.SetRenderer()`, allowing us to use any templating engine.
Below is an example using Go `html/template`
Implement `echo.Render`
```go
Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
}
```
Pre-compile templates
```go
t := &Template{
templates: template.Must(template.ParseGlob("public/views/*.html")),
}
```
Register templates
```go
e := echo.New()
e.SetRenderer(t)
e.Get("/hello", Hello)
```
Template `public/views/hello.html`
```html
{{define "hello"}}Hello, {{.}}!{{end}}
```
Handler
```go
func Hello(c *echo.Context) error {
return c.Render(http.StatusOK, "hello", "World")
}
```
### JSON ### JSON
```go ```go
context.JSON(code int, v interface{}) error Context.JSON(code int, v interface{}) error
``` ```
Sends a JSON HTTP response with status code. Sends a JSON HTTP response with status code.
### String ### XML
```go ```go
context.String(code int, s string) error Context.XML(code int, v interface{}) error
``` ```
Sends a text/plain HTTP response with status code. Sends an XML HTTP response with status code.
### HTML ### HTML
```go ```go
func (c *Context) HTML(code int, html string) error Context.HTML(code int, html string) error
``` ```
Sends an HTML HTTP response with status code. Sends an HTML HTTP response with status code.
### String
```go
Context.String(code int, s string) error
```
Sends a text/plain HTTP response with status code.
### Static files ### Static files
`Echo.Static(path, root string)` serves static files. For example, code below serves `Echo.Static(path, root string)` serves static files. For example, code below serves