text/template package for server-side rendering. Load templates once at startup with app.LoadHTMLGlob(), then render them from any handler with ctx.HTML().
Loading Templates
Callapp.LoadHTMLGlob(pattern) before starting the server. The pattern follows the same glob syntax as filepath.Glob.
Custom Template Functions
Register custom functions withapp.SetFuncMap() before calling app.LoadHTMLGlob(). The function map is applied when templates are parsed.
Rendering a Template
Callctx.HTML(code, name, data) from a handler. name is the template file name as registered by the glob, and data is any value passed as the template’s dot (.).
500 and the error message as plain text.
Complete Example
1
Create the template file
templates/index.tmpl:2
Write the server code
main.go:3
Run and visit the page