Basic information
About templ-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'.
templ-htmx is an agent skill listed in the AddMCP.net directory, published by Xe. Runtime: Go. License: CC0-1.0. The repository has 337 GitHub stars. Source code is hosted on github.com. Related topics: discord, dokku, go, license, minecraft.
Overview
What is templ-htmx?
name: templ-htmx description: 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'.
Templ + HTMX Integration
Overview
HTMX enables modern, interactive web applications with minimal JavaScript. Combined with templ's type-safe components, you get fast, reliable hypermedia-driven UIs.
Key Benefits:
- No JavaScript framework needed
- Server-side rendering
- Minimal client-side code
- Progressive enhancement
- Type-safe components
When to Use This Skill
Use when:
- Building interactive UIs
- Creating dynamic content
- User mentions "HTMX", "dynamic updates", "real-time"
- Implementing AJAX-like behavior without JS
- Building SPAs without frameworks
Quick Start
@htmx.Use()
{ children... } } ``` The `htmx.Use()` component includes the core HTMX library. You can add extensions: ```templ // Add SSE and path-params extensions @htmx.Use("sse", "path-params") ``` Available extensions: `event-header`, `path-params`, `remove-me`, `websocket`. ### 3. Detect HTMX Requests Use the `htmx.Is()` function to check if a request was made by HTMX: ```go import "within.website/x/htmx" func handler(w http.ResponseWriter, r *http.Request) { if htmx.Is(r) { // Return partial HTML fragment for HTMX components.Partial().Render(r.Context(), w) } else { // Return full page for direct navigation components.FullPage().Render(r.Context(), w) } } ``` ### 4. Create Interactive Component ```templ templ Counter(count int) {Count: { strconv.Itoa(count) }
IncrementCore HTMX Attributes
Common Patterns
-
for _, result := range results {
- { result } }
Users online: { strconv.Itoa(stats.UsersOnline) }
Active sessions: { strconv.Itoa(stats.Sessions) }
Advanced Patterns
Response Headers
HX-Trigger
Trigger client-side events:
func handler(w http.ResponseWriter, r *http.Request) {
// Do work...
// Trigger custom event
w.Header().Set("HX-Trigger", "itemCreated")
components.Success().Render(r.Context(), w)
}
Client side:
document.body.addEventListener("itemCreated", function (evt) {
console.log("Item created!");
});
HX-Redirect
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("HX-Redirect", "/dashboard")
w.WriteHeader(http.StatusOK)
}
HX-Refresh
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("HX-Refresh", "true")
w.WriteHeader(http.StatusOK)
}
Special Status Code
Stop polling with status code 286:
import "within.website/x/htmx"
func pollHandler(w http.ResponseWriter, r *http.Request) {
if shouldStopPolling() {
w.WriteHeader(htmx.StatusStopPolling)
return
}
// ... return normal content
}
Request Headers
Access HTMX request headers:
import "within.website/x/htmx"
func handler(w http.ResponseWriter, r *http.Request) {
// Check if this is an HTMX request
if htmx.Is(r) {
// Get user's response to hx-prompt
promptResponse := r.Header.Get(htmx.HeaderPrompt)
}
}Best Practices
- Keep handlers focused - Return only the HTML fragment needed
- Use semantic HTML - Works without JS
- Handle errors gracefully - Return error components
- Optimize responses - Send minimal HTML
- Use OOB for multi-updates - Update multiple page sections
- Progressive enhancement - Always provide fallback
Full Example: Todo App
My Todos
@TodoForm() @TodoList(todos)-
for _, todo := range todos {
@TodoItem(todo)
}
Next Steps
- Style components → Use
templ-cssskill - Deploy → Use
templ-deploymentskill - Test → Use
templ-testingskill
Comments
No comments yet
Be the first to share feedback about this listing. Sign in to leave a comment.
Sign In