mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
2507dc13e9
Signed-off-by: Vishal Rana <vr@labstack.com>
1.2 KiB
1.2 KiB
title | menu | ||||||
---|---|---|---|---|---|---|---|
Templates |
|
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
using Echo.SetRenderer()
, allowing us to use any template engine.
Example below shows how to use Go html/template
:
-
Implement
echo.Renderer
interfacetype Template struct { templates *template.Template } func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { return t.templates.ExecuteTemplate(w, name, data) }
-
Pre-compile templates
public/views/hello.html
{{define "hello"}}Hello, {{.}}!{{end}}
t := &Template{ templates: template.Must(template.ParseGlob("public/views/*.html")), }
-
Register templates
e := echo.New() e.SetRenderer(t) e.GET("/hello", Hello)
-
Render a template inside your handler
func Hello(c echo.Context) error { return c.Render(http.StatusOK, "hello", "World") }