1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-26 03:20:08 +02:00

Renderer example

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-10 21:48:26 -07:00
parent 6633a1fca7
commit 670e21d80e
5 changed files with 127 additions and 31 deletions

View File

@ -30,28 +30,40 @@ Echo is a fast HTTP router (zero memory allocation) + micro web framework in Go.
package main package main
import ( import (
"io"
"net/http" "net/http"
"html/template"
"github.com/labstack/echo" "github.com/labstack/echo"
mw "github.com/labstack/echo/middleware" mw "github.com/labstack/echo/middleware"
"github.com/rs/cors" "github.com/rs/cors"
"github.com/thoas/stats" "github.com/thoas/stats"
) )
type user struct { type (
ID string `json:"id"` // Template provides HTML template rendering
Name string `json:"name"` Template struct {
templates *template.Template
}
user struct {
ID string `json:"id"`
Name string `json:"name"`
}
)
var (
users map[string]user
)
// Render HTML
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
} }
var users map[string]user func welcome(c *echo.Context) {
c.Render("welcome", "Joe")
func init() {
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
}
} }
func createUser(c *echo.Context) { func createUser(c *echo.Context) {
@ -109,6 +121,16 @@ func main() {
e.Get("/users", getUsers) e.Get("/users", getUsers)
e.Get("/users/:id", getUser) e.Get("/users/:id", getUser)
//***************//
// Templates //
//***************//
t := &Template{
// Cached templates
templates: template.Must(template.ParseFiles("public/tpl/welcome.tpl")),
}
e.Renderer(t)
e.Get("/welcome", welcome)
//***********// //***********//
// Group // // Group //
//***********// //***********//
@ -132,6 +154,15 @@ func main() {
// Start server // Start server
e.Run(":8080") e.Run(":8080")
} }
func init() {
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
}
}
``` ```
### Benchmark ### Benchmark

View File

@ -45,9 +45,14 @@ func (c *Context) Bind(v interface{}) error {
return ErrUnsupportedMediaType return ErrUnsupportedMediaType
} }
func (c *Context) Render(code int, name string, data interface{}) error { // Render calls the registered HTML template renderer and sends a text/html
// response.
func (c *Context) Render(name string, data interface{}) error {
if c.echo.renderer == nil {
return ErrNoRenderer
}
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8") c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
c.Response.WriteHeader(code) c.Response.WriteHeader(http.StatusOK)
return c.echo.renderer.Render(c.Response, name, data) return c.echo.renderer.Render(c.Response, name, data)
} }

View File

@ -3,11 +3,23 @@ package echo
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"text/template"
) )
type (
Template struct {
templates *template.Template
}
)
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func TestContext(t *testing.T) { func TestContext(t *testing.T) {
b, _ := json.Marshal(u1) b, _ := json.Marshal(u1)
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b)) r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
@ -16,6 +28,7 @@ func TestContext(t *testing.T) {
Request: r, Request: r,
params: make(Params, 5), params: make(Params, 5),
store: make(store), store: make(store),
echo: &Echo{},
} }
//**********// //**********//
@ -66,24 +79,38 @@ func TestContext(t *testing.T) {
t.Error("user name should be Joe") t.Error("user name should be Joe")
} }
// Render
tpl := &Template{
templates: template.Must(template.New("hello").Parse("{{.}}")),
}
c.echo.renderer = tpl
if err := c.Render("hello", "Joe"); err != nil {
t.Errorf("render %v", err)
}
c.echo.renderer = nil
if err := c.Render("hello", "Joe"); err == nil {
t.Error("render should error out")
}
// JSON // JSON
r.Header.Set(HeaderAccept, MIMEJSON) r.Header.Set(HeaderAccept, MIMEJSON)
c.Response.committed = false
if err := c.JSON(http.StatusOK, u1); err != nil { if err := c.JSON(http.StatusOK, u1); err != nil {
t.Errorf("render json %v", err) t.Errorf("json %v", err)
} }
// String // String
r.Header.Set(HeaderAccept, MIMEText) r.Header.Set(HeaderAccept, MIMEText)
c.Response.committed = false c.Response.committed = false
if err := c.String(http.StatusOK, "Hello, World!"); err != nil { if err := c.String(http.StatusOK, "Hello, World!"); err != nil {
t.Errorf("render string %v", err) t.Errorf("string %v", err)
} }
// HTML // HTML
r.Header.Set(HeaderAccept, MIMEHTML) r.Header.Set(HeaderAccept, MIMEHTML)
c.Response.committed = false c.Response.committed = false
if err := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); err != nil { if err := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); err != nil {
t.Errorf("render html %v", err) t.Errorf("html %v", err)
} }
// Redirect // Redirect

View File

@ -65,6 +65,7 @@ var (
// Errors // Errors
ErrUnsupportedMediaType = errors.New("echo: unsupported media type") ErrUnsupportedMediaType = errors.New("echo: unsupported media type")
ErrNoRenderer = errors.New("echo: renderer not registered")
) )
// New creates an Echo instance. // New creates an Echo instance.
@ -109,12 +110,13 @@ func (e *Echo) MaxParam(n uint8) {
e.maxParam = n e.maxParam = n
} }
// NotFoundHandler sets a custom NotFound handler. // NotFoundHandler registers a custom NotFound handler.
func (e *Echo) NotFoundHandler(h Handler) { func (e *Echo) NotFoundHandler(h Handler) {
e.notFoundHandler = wrapH(h) e.notFoundHandler = wrapH(h)
} }
// Renderer sets an HTML Renderer. // Renderer registers an HTML template renderer, it is used by
// echo.Context.Render API.
func (e *Echo) Renderer(r Renderer) { func (e *Echo) Renderer(r Renderer) {
e.renderer = r e.renderer = r
} }

View File

@ -1,28 +1,40 @@
package main package main
import ( import (
"io"
"net/http" "net/http"
"html/template"
"github.com/labstack/echo" "github.com/labstack/echo"
mw "github.com/labstack/echo/middleware" mw "github.com/labstack/echo/middleware"
"github.com/rs/cors" "github.com/rs/cors"
"github.com/thoas/stats" "github.com/thoas/stats"
) )
type user struct { type (
ID string `json:"id"` // Template provides HTML template rendering
Name string `json:"name"` Template struct {
templates *template.Template
}
user struct {
ID string `json:"id"`
Name string `json:"name"`
}
)
var (
users map[string]user
)
// Render HTML
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
} }
var users map[string]user func welcome(c *echo.Context) {
c.Render("welcome", "Joe")
func init() {
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
}
} }
func createUser(c *echo.Context) { func createUser(c *echo.Context) {
@ -80,6 +92,16 @@ func main() {
e.Get("/users", getUsers) e.Get("/users", getUsers)
e.Get("/users/:id", getUser) e.Get("/users/:id", getUser)
//***************//
// Templates //
//***************//
t := &Template{
// Cached templates
templates: template.Must(template.ParseFiles("public/tpl/welcome.tpl")),
}
e.Renderer(t)
e.Get("/welcome", welcome)
//***********// //***********//
// Group // // Group //
//***********// //***********//
@ -103,3 +125,12 @@ func main() {
// Start server // Start server
e.Run(":8080") e.Run(":8080")
} }
func init() {
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
}
}