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:
parent
ace5843983
commit
e550355a5f
@ -106,8 +106,8 @@ func (c *Context) Bind(i interface{}) error {
|
||||
return c.echo.binder(c.request, i)
|
||||
}
|
||||
|
||||
// Render invokes the registered HTML template renderer and sends a text/html
|
||||
// response with status code.
|
||||
// Render renders a template with data and sends a text/html response with status
|
||||
// code. Templates can be registered using `Echo.SetRenderer()`.
|
||||
func (c *Context) Render(code int, name string, data interface{}) (err error) {
|
||||
if c.echo.renderer == nil {
|
||||
return RendererNotRegistered
|
||||
|
@ -46,6 +46,10 @@ and message `HTTPError.Message`.
|
||||
|
||||
Enables debug mode.
|
||||
|
||||
### Disable colored log
|
||||
|
||||
`Echo.DisableColoredLog()`
|
||||
|
||||
## Routing
|
||||
|
||||
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
|
||||
|
||||
### 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
|
||||
|
||||
```go
|
||||
context.JSON(code int, v interface{}) error
|
||||
Context.JSON(code int, v interface{}) error
|
||||
```
|
||||
|
||||
Sends a JSON HTTP response with status code.
|
||||
|
||||
### String
|
||||
### XML
|
||||
|
||||
```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
|
||||
|
||||
```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.
|
||||
|
||||
### String
|
||||
|
||||
```go
|
||||
Context.String(code int, s string) error
|
||||
```
|
||||
|
||||
Sends a text/plain HTTP response with status code.
|
||||
|
||||
### Static files
|
||||
|
||||
`Echo.Static(path, root string)` serves static files. For example, code below serves
|
||||
|
Loading…
Reference in New Issue
Block a user