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

Resources