templ-http
Templ HTTP Integration
Use progressive disclosure: begin with the handler pattern, then add routing and middleware only as needed.
Level 1: Minimal Integration
Use this skill when serving templ from net/http.
- Parse request input.
- Build view model/data.
- Render templ component with
r.Context(). - Handle render errors and status codes.
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
}
}
Level 2: Routing and Request Data
- Routes: start with
http.NewServeMux()and explicit handlers. - Methods: branch on
r.Methodwhen endpoint serves multiple actions. - Input sources: query (
r.URL.Query()), form (r.ParseForm()+FormValue), path segments. - Status discipline: set status before rendering non-200 responses.
- Post/Redirect/Get: redirect after successful form POSTs.
Level 3: Production Patterns
- Add middleware for auth/logging/request IDs.
- Return component-based error pages for consistent UX.
- Serve static files with
http.FileServerand a dedicated prefix. - Keep handler functions thin; move business logic into services.
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)
}
}
Escalate to Other Skills
- Need reusable view APIs: use
templ-components. - Need HTMX partial responses: use
templ-htmx. - Need syntax corrections in
.templ: usetempl-syntax.
References
- Handlers and routing:
resources/handlers-and-routing.md - Middleware and errors:
resources/middleware-and-errors.md - Request data and response patterns:
resources/request-and-response-patterns.md - Go
net/http: https://pkg.go.dev/net/http - templ server-side rendering: https://templ.guide/server-side-rendering/
More from xe/skills
xe-go-style
Write Go code following the conventions and patterns used in the within.website/x repository, including CLI patterns, error handling, logging with slog, HTTP handlers, and testing.
6gorm-dao
Write GORM data access code using the DAO (Data Access Object) pattern. Use when creating database models, writing queries, setting up GORM, adding CRUD methods, or working with gorm.io in Go services. Also use when the user mentions "DAO", "data access", "ORM", "database models", "GORM", or is building a Go service that talks to a relational database.
4templ-components
Create reusable templ UI components with props, children, and composition patterns. Use when building UI components, creating component libraries, mentions 'button component', 'card component', or 'reusable templ components'.
4templ-syntax
Learn and write templ component syntax including expressions, conditionals, loops, and Go integration. Use when writing .templ files, learning templ syntax, or mentions 'templ component', 'templ expressions', or '.templ file syntax'.
4templ-htmx
Build interactive hypermedia-driven applications with templ and HTMX. Use when creating dynamic UIs, real-time updates, AJAX interactions, mentions 'HTMX', 'dynamic content', or 'interactive templ app'.
4xe-writing-style
Transform unstructured notes into polished blog posts in Xe Iaso's voice. Use
1