Templ
templ is a language, CLI, and runtime for building HTML UIs in Go. You write .templ files, run templ generate, and get ordinary Go functions that render type-safe HTML. Current line: v0.3.x. Requires Go 1.24+.
Overview
- What: HTML components compiled to plain Go functions
- Install:
go install github.com/a-h/templ/cmd/templ@latest - Workflow: write
hello.templ->templ generate->hello_templ.go-> call it like any Go function - Core type:
templ.Component, a one-method interface:Render(ctx context.Context, w io.Writer) error - Escaping: expression output is HTML-escaped automatically
- Interactivity: pairs with htmx or Datastar via fragment rendering; no client runtime of its own
- Deploy: a plain Go binary or a small distroless container
Why templ
Go’s html/template gives you strings with holes, parsed at runtime. templ gives you functions: components take typed arguments, and wrong argument types fail at build time, not on the first request.
templ greeting(name string) {
<p>Hello, { name }</p>
}
// generated by templ generate
func greeting(name string) templ.Component
Because Render writes to any io.Writer, the same component can serve an HTTP response, write a static file, or render into a test buffer. templ is deliberately small: no router, no asset pipeline, no client runtime. You bring the Go stdlib plus whatever else you already use.
Good fit: server-rendered sites and apps shipped as a single Go binary, HTML-over-the-wire UIs with htmx, embedded admin panels.
Poor fit: heavy client-side SPAs that need a large JavaScript component ecosystem.
Contents
- Concepts
- Components and Syntax - component model, expressions, control flow, composition, templ.Join/KV/Once/Raw
- Rendering Model - the generate pipeline, Component interface, Handler options, streaming
- Security and Scripts - auto-escaping, script templates, JSONScript
- Quickstart
- First App - install, generate, and serve a first component
- How-To
- Use with htmx - hx-* attributes and native fragment rendering
- Style with Tailwind - class attributes, LSP setup, css components
- Cache Rendered Pages - roll-your-own page cache and Cache-Control
- Deploy in Docker - multi-stage distroless image, static assets
- Deep-Dives
- Generated Code - what
*_templ.gocontains and why it matters
- Generated Code - what
- Troubleshooting
- Common Errors - error catalog with causes and fixes
- Notes
- Ecosystem - component libraries, interactivity, tooling
Resources
- templ.guide - official documentation
- a-h/templ - source repository