Skip to content

Commit e550355

Browse files
committed
Update docs with new API
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent ace5843 commit e550355

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func (c *Context) Bind(i interface{}) error {
106106
return c.echo.binder(c.request, i)
107107
}
108108

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

website/docs/guide.md

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ and message `HTTPError.Message`.
4646

4747
Enables debug mode.
4848

49+
### Disable colored log
50+
51+
`Echo.DisableColoredLog()`
52+
4953
## Routing
5054

5155
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
299303

300304
## Response
301305

306+
### Template
307+
308+
```go
309+
Context.Render(code int, name string, data interface{}) error
310+
```
311+
Renders a template with data and sends a text/html response with status code. Templates
312+
can be registered using `Echo.SetRenderer()`, allowing us to use any templating engine.
313+
Below is an example using Go `html/template`
314+
315+
Implement `echo.Render`
316+
317+
```go
318+
Template struct {
319+
templates *template.Template
320+
}
321+
322+
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
323+
return t.templates.ExecuteTemplate(w, name, data)
324+
}
325+
```
326+
327+
Pre-compile templates
328+
329+
```go
330+
t := &Template{
331+
templates: template.Must(template.ParseGlob("public/views/*.html")),
332+
}
333+
```
334+
335+
Register templates
336+
337+
```go
338+
e := echo.New()
339+
e.SetRenderer(t)
340+
e.Get("/hello", Hello)
341+
```
342+
343+
Template `public/views/hello.html`
344+
345+
```html
346+
{{define "hello"}}Hello, {{.}}!{{end}}
347+
348+
```
349+
350+
Handler
351+
352+
```go
353+
func Hello(c *echo.Context) error {
354+
return c.Render(http.StatusOK, "hello", "World")
355+
}
356+
```
357+
302358
### JSON
303359

304360
```go
305-
context.JSON(code int, v interface{}) error
361+
Context.JSON(code int, v interface{}) error
306362
```
307363

308364
Sends a JSON HTTP response with status code.
309365

310-
### String
366+
### XML
311367

312368
```go
313-
context.String(code int, s string) error
369+
Context.XML(code int, v interface{}) error
314370
```
315371

316-
Sends a text/plain HTTP response with status code.
372+
Sends an XML HTTP response with status code.
317373

318374
### HTML
319375

320376
```go
321-
func (c *Context) HTML(code int, html string) error
377+
Context.HTML(code int, html string) error
322378
```
323379

324380
Sends an HTML HTTP response with status code.
325381

382+
### String
383+
384+
```go
385+
Context.String(code int, s string) error
386+
```
387+
388+
Sends a text/plain HTTP response with status code.
389+
326390
### Static files
327391

328392
`Echo.Static(path, root string)` serves static files. For example, code below serves

0 commit comments

Comments
 (0)