Templates | Echo

Context#Render(code int, name string, data any) 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.

e.Renderer = &echo.TemplateRenderer{
Template: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
}
type Template struct {
templates *template.Template
}

func (t *Template) Render(c *echo.Context, w io.Writer, name string, data any) error {
return t.templates.ExecuteTemplate(w, name, data)
}

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]any and augmenting this object in the custom renderer. Given the flexibility of the latter approach, here is a sample program:

package main

import (
"html/template"
"io"
"net/http"

"github.com/labstack/echo/v5"
)

// TemplateRenderer is a custom html/template renderer for Echo framework
type TemplateRenderer struct {
templates *template.Template
}

// Render renders a template document
func (t *TemplateRenderer) Render(c *echo.Context, w io.Writer, name string, data any) error {

// Add global methods if data is a map
if viewContext, isMap := data.(map[string]any); isMap {
viewContext["reverse"] = c.RouteInfo().Reverse
}

return t.templates.ExecuteTemplate(w, name, data)
}

func main() {
e := echo.New()
e.Renderer = &TemplateRenderer{
templates: template.Must(template.ParseGlob("main/*.html")),
}

e.GET("/something/:name", func(c *echo.Context) error {
return c.Render(http.StatusOK, "template.html", map[string]any{
"name": "Dolly!",
})
})

if err := e.Start(":1323"); err != nil {
e.Logger.Error("shutting down the server", "error", err)
}
}