Why HTMX?
- No JavaScript - Server renders HTML, HTMX handles interactivity
- Progressive enhancement - Works without JS enabled
- Simpler architecture - No separate frontend build
- Smaller bundles - HTMX is ~14KB minified
Setup
The default layout includes HTMX from CDN:Core Concepts
Loading Data
Usehx-get to load content:
/api/users and replaces the div content.
Form Submission
Usehx-post for forms:
Delete Operations
HTMX Attributes
| Attribute | Description |
|---|---|
hx-get | GET request to URL |
hx-post | POST request to URL |
hx-put | PUT request to URL |
hx-delete | DELETE request to URL |
hx-trigger | When to trigger (load, click, etc.) |
hx-target | Element to update with response |
hx-swap | How to swap content (innerHTML, outerHTML, etc.) |
hx-confirm | Show confirmation dialog |
Server-Side Patterns
Detecting HTMX Requests
Returning HTML Fragments
Using templ for Partials
Common Patterns
Load on Page Load
Click to Load More
Form with Reset
Toggle Checkbox
Delete with Confirmation
Infinite Scroll
Search with Debounce
Polling
Full Example: Task Manager
Page Template
API Routes
Toggle Route
Best Practices
Return minimal HTML
Return minimal HTML
Only return the HTML that needs to change, not the entire page.
Use meaningful IDs
Use meaningful IDs
Give your target elements clear, descriptive IDs.
Handle loading states
Handle loading states
Use
hx-indicator to show loading spinners.Validate on server
Validate on server
Always validate input on the server, even with client hints.