Render renders a template with data and sends a text/html response with status code. Renderer must be registered using `Echo.Renderer`.
(code int, name string, data any)
| 491 | // Render renders a template with data and sends a text/html response with status |
| 492 | // code. Renderer must be registered using `Echo.Renderer`. |
| 493 | func (c *Context) Render(code int, name string, data any) (err error) { |
| 494 | if c.echo.Renderer == nil { |
| 495 | return ErrRendererNotRegistered |
| 496 | } |
| 497 | // as Renderer.Render can fail, and in that case we need to delay sending status code to the client until |
| 498 | // (global) error handler decides the correct status code for the error to be sent to the client, so we need to write |
| 499 | // the rendered template to the buffer first. |
| 500 | // |
| 501 | // html.Template.ExecuteTemplate() documentations writes: |
| 502 | // > If an error occurs executing the template or writing its output, |
| 503 | // > execution stops, but partial results may already have been written to |
| 504 | // > the output writer. |
| 505 | |
| 506 | buf := new(bytes.Buffer) |
| 507 | if err = c.echo.Renderer.Render(c, buf, name, data); err != nil { |
| 508 | return |
| 509 | } |
| 510 | return c.HTMLBlob(code, buf.Bytes()) |
| 511 | } |
| 512 | |
| 513 | // HTML sends an HTTP response with status code. |
| 514 | func (c *Context) HTML(code int, html string) (err error) { |