Integrate templ components with Go HTTP server using net/http. Use when connecting templ to web server, creating HTTP handlers, mentions 'templ server', 'HTTP routes', or 'serve templ components'.
Use progressive disclosure: begin with the handler pattern, then add routing and middleware only as needed.
Use this skill when serving templ from net/http.
r.Context().func homeHandler(w http.ResponseWriter, r *http.Request) {
if err := components.HomePage().Render(r.Context(), w); err != nil {
http.Error(w, "render failed", http.StatusInternalServerError)
return
}
}
http.NewServeMux() and explicit handlers.r.Method when endpoint serves multiple actions.r.URL.Query()), form (r.ParseForm() + FormValue), path segments.http.FileServer and a dedicated prefix.func usersHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
users := listUsers(r.Context())
_ = components.UserList(users).Render(r.Context(), w)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
templ-components.templ-htmx..templ: use templ-syntax.resources/handlers-and-routing.mdresources/middleware-and-errors.mdresources/request-and-response-patterns.mdnet/http: https://pkg.go.dev/net/http