Templates

Template Rendering

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 by setting Echo.Renderer, allowing us to use any template engine.

Example below shows how to use Go html/template:

  1. Implement echo.Renderer interface

    1. type Template struct {
    2. templates *template.Template
    3. }
    4. func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
    5. return t.templates.ExecuteTemplate(w, name, data)
    6. }
  2. Pre-compile templates

    public/views/hello.html

    1. {{define "hello"}}Hello, {{.}}!{{end}}
    1. t := &Template{
    2. templates: template.Must(template.ParseGlob("public/views/*.html")),
    3. }
  3. Register templates

    1. e := echo.New()
    2. e.Renderer = t
    3. e.GET("/hello", Hello)
  4. Render a template inside your handler

    1. func Hello(c echo.Context) error {
    2. return c.Render(http.StatusOK, "hello", "World")
    3. }

Advanced - Calling Echo from templates

In certain situations it might be useful to generate URIs from the templates. In order to do so, you need to call Echo#Reverse from the templates itself. Golang’s html/template package is not the best suited for this job, but this can be done in two ways: by providing a common method on all objects passed to templates or by passing map[string]interface{} and augmenting this object in the custom renderer. Given the flexibility of the latter approach, here is a sample program:

template.html

  1. <html>
  2. <body>
  3. <h1>Hello {{index . "name"}}</h1>
  4. <p>{{ with $x := index . "reverse" }}
  5. {{ call $x "foobar" }} &lt;-- this will call the $x with parameter "foobar"
  6. {{ end }}
  7. </p>
  8. </body>
  9. </html>

server.go

  1. package main
  2. import (
  3. "html/template"
  4. "io"
  5. "log"
  6. "net/http"
  7. "github.com/labstack/echo"
  8. )
  9. // TemplateRenderer is a custom html/template renderer for Echo framework
  10. type TemplateRenderer struct {
  11. templates *template.Template
  12. }
  13. // Render renders a template document
  14. func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
  15. // Add global methods if data is a map
  16. if viewContext, isMap := data.(map[string]interface{}); isMap {
  17. viewContext["reverse"] = c.Echo().Reverse
  18. }
  19. return t.templates.ExecuteTemplate(w, name, data)
  20. }
  21. func main() {
  22. e := echo.New()
  23. renderer := &TemplateRenderer{
  24. templates: template.Must(template.ParseGlob("*.html")),
  25. }
  26. e.Renderer = renderer
  27. // Named route "foobar"
  28. e.GET("/something", func(c echo.Context) error {
  29. return c.Render(http.StatusOK, "something.html", map[string]interface{}{
  30. "name": "Dolly!",
  31. })
  32. }).Name = "foobar"
  33. log.Fatal(e.Start(":8000"))
  34. }