First App

From zero to a served HTML page: install the tooling, write one component, generate, serve it with net/http.

Install

Go 1.24+ required. Install the CLI and add the runtime dependency:

go install github.com/a-h/templ/cmd/templ@latest
go get github.com/a-h/templ@latest

Alternative: keep the CLI project-local with a Go tool directive, then run it via go tool:

go get -tool github.com/a-h/templ/cmd/templ@latest
go tool templ generate

Prebuilt binaries and a Nix flake are also published if you prefer not to build from source.

Write a component

components.templ:

package main

templ hello(name string) {
    <h1>Hello, { name }</h1>
}

Generate

templ generate

This writes components_templ.go next to the source file. It is ordinary Go that the Go compiler needs - commit it, or generate it in CI before building.

Render it

First to stdout, just to see it work (main.go):

package main

import (
    "context"
    "os"
)

func main() {
    err := hello("templ").Render(context.Background(), os.Stdout)
    if err != nil {
        panic(err)
    }
}

Then over HTTP with templ.Handler:

package main

import (
    "net/http"
)

func main() {
    mux := http.NewServeMux()
    mux.Handle("/", templ.Handler(hello("templ")))
    http.ListenAndServe(":8080", mux)
}
go run .
curl localhost:8080

Live reload loop

Run the generator in watch mode with a proxy and a rebuild command:

templ generate --watch --proxy http://localhost:8080 --cmd "go run ."

Open the proxy URL in your browser instead of the app directly. On each save, templ regenerates, reruns --cmd, and reloads the page.

Gotcha: If the page shows old markup, the generated file is stale - run templ generate. In development, keep --watch running so you never think about it.

Next: Components and Syntax for the full language, or Use with htmx to add interactivity.