htmx : interfaces web hypermédia

VérifiéSûr

Guide d'utilisation de htmx pour créer des interfaces web dynamiques en HTML pur : attributs AJAX, stratégies de swap, SSE et gestion des réponses.

Spar Skills Guide Bot
DeveloppementIntermédiaire
17/11/2026
Claude CodeCursorWindsurfCopilotCodex
#htmx#hypermedia#ajax#server-rendered#html

Recommandé pour

Synthèse automatique

Un guide pratique pour utiliser htmx 2.x et ses attributs HTML afin d'ajouter des requêtes AJAX, des échanges de fragments et des déclencheurs à une application rendue côté serveur.

Points forts

  • Montre l'installation via CDN avec une version épinglée et précise la distinction entre htmx 2.x et la version 4.0 publiée sous le tag next.
  • Explique comment configurer responseHandling pour remplacer le contenu même sur une réponse 422, un piège fréquent avec les formulaires.
  • Couvre les attributs essentiels, les stratégies de swap dont out-of-band, les déclencheurs avec debounce/intersect et les extensions SSE/WebSocket.
  • Fournit des exemples HTML directement copiables pour des cas concrets : liste, formulaire, suppression, recherche et scroll infini.

Limites

  • Reste centré sur le client : aucun exemple de route serveur, de génération de fragments ou d'intégration avec un framework backend.
  • N'aborde pas la sécurité (CSRF, XSS), l'accessibilité ni le rendu progressif, pourtant importants en production.
  • Les exemples sont des extraits isolés plutôt qu'un projet complet ou un guide de migration.
Quand l'utiliser

À utiliser pour ajouter des mises à jour partielles, de la recherche instantanée, du scroll infini ou des flux SSE/WebSocket à une application web rendue côté serveur.

Quand l'éviter

À éviter pour une SPA fortement côté client ou si le backend ne peut pas renvoyer des fragments HTML.

Analyse de sécurité

Sûr
Score qualité84/100

The skill is pure documentation/tutorial content for htmx. It contains no executable scripts run by the agent, no bash/docker/network commands, no credential handling, and no destructive instructions. Its security guidance is actually sound (escape fragments, keep selfRequestsOnly, disable allowEval, send CSRF headers, pin versions with integrity hashes). The only external dependency is a CDN script tag in HTML examples, which is normal web practice and is pinned + SRI-protected.

Points d'attention
  • •Only informational: examples load htmx/extension bundles from a third-party CDN (jsdelivr); mitigated by pinned versions and SRI/crossorigin, but the documented integrity hash is presented as authoritative and should be verified against the upstream release.
Comment cette analyse est faite

Exemples

Load a list without reload
Add htmx 2.x to my Express-rendered page and make the 'Load Articles' button fetch /api/articles and replace the contents of #article-list.
Swap 422 validation errors
My htmx form gets a 422 response with an error fragment but the page does not update. Show me the htmx-config responseHandling setup and the form markup to swap that fragment.
Infinite scroll
Implement infinite scroll in htmx using hx-trigger='intersect once' to append the next page of articles after the current list.

name: htmx description: | htmx gives you access to AJAX, CSS transitions, WebSockets, and Server-Sent Events directly in HTML using attributes. It enables dynamic web UIs without writing JavaScript by letting any element issue HTTP requests and swap content into the DOM. license: Apache-2.0 compatibility: 'any web server, html' metadata: author: terminal-skills version: "1.1.0" repository: https://github.com/bigskysoftware/htmx category: development tags: "html, hypermedia, ajax, sse"

htmx

htmx extends HTML with attributes like hx-get, hx-post, hx-swap, and hx-trigger to make any element capable of issuing HTTP requests and updating the DOM. The server returns HTML fragments, not JSON.

Overview

htmx 2.x lets plain HTML attributes issue AJAX requests, listen for events and swap server-rendered fragments into the page. Use it for server-rendered apps (Django, Rails, Go, Express) that need partial updates, search-as-you-type, infinite scroll, polling or live streams, without a JavaScript framework.

Instructions

Installation

<!-- index.html — htmx 2.0.11 from a CDN, pinned with an integrity hash (hash from the htmx docs) -->
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.11/dist/htmx.min.js"
  integrity="sha384-2OatzQy1H+Zd/IIrjr1TcuDGqLXeHhbooAyJY1KdQMKnr4LZ22k31GBLdYKHmVjg"
  crossorigin="anonymous"></script>

htmx.org@latest on npm is 2.x; 4.0 is published only under the next tag and changes defaults, so pin 2.x unless you are migrating on purpose. SSE and WebSocket support are no longer in core: they are separate packages (htmx-ext-sse, htmx-ext-ws) loaded after htmx (see below).

By default htmx does not swap 4xx and 5xx responses, so a 422 validation-error fragment is silently dropped. To swap it, add to <head>:

<meta name="htmx-config" content='{"responseHandling":[{"code":"204","swap":false},{"code":"[23]..","swap":true},{"code":"422","swap":true},{"code":"[45]..","swap":false,"error":true},{"code":"...","swap":true}]}' />

Core Attributes

<!-- templates/core-demo.html — fundamental htmx attributes -->

<!-- GET request, replace inner HTML of target -->
<button hx-get="/api/articles" hx-target="#article-list" hx-swap="innerHTML">
  Load Articles
</button>
<div id="article-list"></div>

<!-- POST form without page reload -->
<form id="article-form" hx-post="/api/articles" hx-target="#article-list" hx-swap="afterbegin">
  <input name="title" placeholder="Title" required />
  <textarea name="body" placeholder="Body" required></textarea>
  <button type="submit">Create</button>
</form>

<!-- DELETE with confirmation -->
<button hx-delete="/api/articles/42" hx-confirm="Are you sure?" hx-target="closest article" hx-swap="outerHTML swap:500ms">
  Delete
</button>

Swap Strategies

<!-- templates/swap-strategies.html — different ways to insert content -->

<!-- Replace inner content (default) -->
<div hx-get="/fragment" hx-swap="innerHTML">Replace my contents</div>

<!-- Replace entire element -->
<div hx-get="/fragment" hx-swap="outerHTML">Replace me entirely</div>

<!-- Append/prepend to list -->
<div id="list">
  <button hx-get="/more" hx-target="#list" hx-swap="beforeend">Load More</button>
</div>

<!-- Out-of-band swaps (update multiple elements) -->
<!-- Server returns: -->
<!-- <div id="notification" hx-swap-oob="innerHTML">New notification!</div> -->
<!-- <div id="count" hx-swap-oob="innerHTML">43</div> -->

Triggers

<!-- templates/triggers.html — custom event triggers -->

<!-- Trigger on input change with debounce -->
<input type="search" name="q"
  hx-get="/search"
  hx-trigger="input changed delay:300ms"
  hx-target="#results" />
<div id="results"></div>

<!-- Trigger on intersection (lazy loading) -->
<div hx-get="/more-articles"
  hx-trigger="intersect once"
  hx-swap="afterend">
  Loading...
</div>

<!-- Trigger on custom event -->
<div hx-get="/notifications" hx-trigger="newMessage from:body">
  Notifications
</div>

<!-- Polling -->
<div hx-get="/api/status" hx-trigger="every 5s">
  Status: checking...
</div>

Server Responses (Python/Django Example)

# views.py — server returns HTML fragments, not JSON
from django.shortcuts import render
from django.http import HttpResponse

def article_list(request):
    articles = Article.objects.filter(published=True)[:20]
    return render(request, "partials/article_list.html", {"articles": articles})

def create_article(request):
    form = ArticleForm(request.POST)
    if form.is_valid():
        article = form.save()
        return render(request, "partials/article_card.html", {"article": article})
    response = render(request, "partials/article_form.html", {"form": form}, status=422)
    response["HX-Retarget"] = "#article-form"
    response["HX-Reswap"] = "outerHTML"
    return response

def delete_article(request, pk):
    Article.objects.filter(pk=pk).delete()
    return HttpResponse("")  # 200 + empty body removes the element with outerHTML; a 204 is never swapped
<!-- templates/partials/article_card.html — HTML fragment returned by server -->
<article id="article-{{ article.id }}">
  <h2>{{ article.title }}</h2>
  <p>{{ article.body|truncatewords:30 }}</p>
  <button hx-delete="/api/articles/{{ article.id }}"
    hx-target="#article-{{ article.id }}"
    hx-swap="outerHTML swap:300ms"
    hx-confirm="Delete this article?">
    Delete
  </button>
</article>

Indicators

<!-- templates/indicators.html — loading indicators -->

<!-- Show spinner during request -->
<button hx-get="/slow-endpoint" hx-indicator="#spinner">
  Load Data
</button>
<span id="spinner" class="htmx-indicator">Loading...</span>

<!-- CSS (htmx adds .htmx-request class during requests) -->
<style>
  .htmx-indicator { display: none; }
  .htmx-request .htmx-indicator { display: inline; }
  .htmx-request.htmx-indicator { display: inline; }
</style>

Headers and Request Config

<!-- templates/request-config.html — request customization -->

<!-- Include extra values -->
<button hx-post="/api/vote" hx-vals='{"article_id": 42, "vote": "up"}'>
  Upvote
</button>

<!-- Include values from other elements -->
<input id="search-input" name="q" />
<button hx-get="/search" hx-include="#search-input">Search</button>

<!-- Push URL to browser history -->
<a hx-get="/articles/my-article" hx-push-url="true" hx-target="#content">
  My Article
</a>

Server-Sent Events

<!-- templates/sse.html — real-time updates with SSE -->
<!-- load after htmx: <script src="https://cdn.jsdelivr.net/npm/htmx-ext-sse@2.2.4/dist/sse.min.js"></script> -->
<div hx-ext="sse" sse-connect="/events/articles">
  <div sse-swap="newArticle" hx-swap="afterbegin">
    <!-- New articles appear here in real-time -->
  </div>
</div>
# views.py — SSE endpoint
from django.http import StreamingHttpResponse
from django.template.loader import render_to_string

def article_events(request):
    def event_stream():
        for article in listen_for_new_articles():  # your own blocking generator
            html = render_to_string("partials/article_card.html", {"article": article})
            # every line of a multi-line payload needs its own "data:" prefix
            data = "\n".join(f"data: {line}" for line in html.splitlines())
            yield f"event: newArticle\n{data}\n\n"
    return StreamingHttpResponse(event_stream(), content_type="text/event-stream")

WebSocket

<!-- templates/ws.html — WebSocket integration -->
<!-- load after htmx: <script src="https://cdn.jsdelivr.net/npm/htmx-ext-ws@2.0.4/dist/ws.min.js"></script> -->
<div hx-ext="ws" ws-connect="/ws/chat">
  <div id="messages"></div>
  <form id="chat-form" ws-send>
    <input name="message" placeholder="Type a message..." />
    <button type="submit">Send</button>
  </form>
</div>

Boosting (Progressive Enhancement)

<!-- templates/boost.html — make regular links/forms use AJAX -->
<body hx-boost="true">
  <!-- All links and forms in this body now use AJAX -->
  <nav>
    <a href="/articles">Articles</a>  <!-- AJAX navigation -->
    <a href="/about">About</a>
  </nav>
  <main id="content">
    <!-- Content swapped here -->
  </main>
</body>

CSRF and Security

<!-- Django: send the CSRF token with every htmx request -->
<body hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
  • Escape user data in every fragment you return (Django and Jinja2 autoescape by default); htmx inserts the HTML as given.
  • Keep htmx.config.selfRequestsOnly at its default true unless you must call other origins, and set allowEval to false if your CSP forbids eval (this disables trigger filters).
  • Detect htmx calls on the server with the HX-Request: true header, and return a full page otherwise. Add Vary: HX-Request when a URL serves both.

Key Patterns

  • Server returns HTML fragments, not JSON — this is hypermedia, not REST
  • Use hx-target to control where responses are inserted; hx-swap controls how
  • Use hx-trigger with modifiers (delay, throttle, changed, once) for precise control
  • Use hx-boost="true" on <body> for easy progressive enhancement of existing sites
  • Use hx-swap-oob for updating multiple page sections from a single response
  • Use hx-push-url for back-button support

Examples

Example 1: Live search box

User request: "Add search-as-you-type to the articles page without writing JavaScript"

Use the debounced input changed delay:300ms trigger from the Triggers section on the search input, return only the matching result rows from /search?q=..., and target #results. Typing "django" sends one request after 300 ms of silence, and the list updates in place.

Example 2: Inline delete with validation errors

User request: "Delete an article from the list and show form errors without a reload"

Use the hx-delete card with hx-target="#article-42" and outerHTML swap; the view returns 200 with an empty body, so the card disappears. For the create form, return the form with status 422 (the view sets HX-Retarget/HX-Reswap so it replaces the form, not the list) and enable 422 swapping with the htmx-config meta tag from Installation, otherwise the errors never appear.

Guidelines

  • Pin the htmx version and use an integrity hash; do not load latest.
  • 4xx/5xx responses are not swapped by default; configure responseHandling or use the response-targets extension for error fragments.
  • A 204 response never swaps; return 200 with an empty body to clear an element.
  • Always escape user data in fragments, and send the CSRF token with hx-headers.

Du fichier au premier essai

Le téléchargement contient exactement le contenu affiché sur cette fiche. Cette copie peut différer de la dernière version du dépôt source.

Consulter la source et ses fichiers annexes

Ce téléchargement contient uniquement SKILL.md. Les scripts, références, modèles et dépendances éventuels ne sont pas inclus. Récupérez les fichiers nécessaires depuis la source en conservant leur arborescence avant de poursuivre.

1. Télécharger le fichier

Télécharger SKILL.md

Depuis la racine d’un projet de test, enregistrez le fichier sous .skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md, ou utilisez la commande suivante. Ce dossier sert à examiner la skill avant son installation.

Commande pour macOS/Linux ou WSL : un shell POSIX et curl sont nécessaires. Sous Windows sans WSL, utilisez le bouton de téléchargement et créez le dossier manuellement.

mkdir -p '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/htmx-hypermedia-driven-web-uis-mwk700a0/download' --output '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md.part' && mv '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md.part' '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md'

2. Vérifier les prérequis

Lisez SKILL.md avant de l’utiliser. Si le fichier cite des scripts ou services externes, examinez-les et préparez les accès nécessaires dans votre environnement de test.

Environnement déclaré par l’auteur : any web server, html

Extrait original : Installation
<!-- index.html — htmx 2.0.11 from a CDN, pinned with an integrity hash (hash from the htmx docs) -->
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.11/dist/htmx.min.js"
  integrity="sha384-2OatzQy1H+Zd/IIrjr1TcuDGqLXeHhbooAyJY1KdQMKnr4LZ22k31GBLdYKHmVjg"
  crossorigin="anonymous"></script>

htmx.org@latest on npm is 2.x; 4.0 is published only under the next tag and changes defaults, so pin 2.x unless you are migrating on purpose. SSE and WebSocket support are no longer in core: they are separate packages (htmx-ext-sse, htmx-ext-ws) loaded after htmx (see below).

By default htmx does not swap 4xx and 5xx responses, so a 422 validation-error fragment is silently dropped. To swap it, add to <head>:

<meta name="htmx-config" content='{"responseHandling":[{"code":"204","swap":false},{"code":"[23]..","swap":true},{"code":"422","swap":true},{"code":"[45]..","swap":false,"error":true},{"code":"...","swap":true}]}' />

Les plateformes de la fiche sont des indications du catalogue. Les chemins ci-dessous suivent la documentation des outils ; ils ne prouvent pas que cette skill y a été exécutée. Aucune version minimale de ces outils n’est attestée pour cette fiche.

3. Charger la skill

Après vérification, copiez le dossier complet dans le projet avec l’une des commandes ci-dessous, puis ouvrez une nouvelle session de l’outil. La commande s’arrête si un dossier du même nom existe déjà : comparez les fichiers avant de le remplacer.

Claude Code
test -f '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md' && test -s '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/htmx' && cp -R '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0' '.claude/skills/htmx'

Cursor
test -f '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md' && test -s '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/htmx' && cp -R '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0' '.cursor/skills/htmx'

Codex
test -f '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md' && test -s '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/htmx' && cp -R '.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0' '.agents/skills/htmx'

OpenSkills

OpenSkills doit déjà être installé. La procédure locale install/read est contrôlée avec OpenSkills 1.5.0, qui exige Node.js ≥ 20.6.0. Aucun slug du catalogue n’est résolu automatiquement.

Installation officielle d’OpenSkills
openskills install './.skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0'

openskills read 'htmx-hypermedia-driven-web-uis-mwk700a0'

La lecture doit afficher le contenu du fichier installé et son dossier de base. Elle ne lance ni la skill ni ses dépendances.

Pour un agent qui lit AGENTS.md, synchronisez ensuite l’index. Cette commande modifie AGENTS.md : relisez le diff avant de l’utiliser.

openskills sync

Mode manuel ou autre outil : joignez SKILL.md à une conversation, ou ouvrez-le dans un agent capable de lire les fichiers. Ajoutez les ressources annexes nécessaires. Il n’existe pas de dossier universel reconnu par tous les outils.

4. Faire un premier essai

Utilisez une entrée courte et fictive : un endpoint d’API, un petit document ou un brief d’interface. Dans une nouvelle conversation, joignez le fichier ou donnez son chemin à l’agent, puis adaptez cette demande :

Lis .skill-downloads/htmx-hypermedia-driven-web-uis-mwk700a0/SKILL.md et applique ses instructions à cet exemple : [décrire une entrée de test]. Indique les fichiers, dépendances ou accès qui manquent. Produis le résultat demandé par la skill sans inventer de données ni exécuter de script non examiné.

Résultat attendu : une sortie correspondant aux instructions de la skill, ou une liste précise des prérequis manquants. Une simple confirmation de lecture ne valide pas le premier usage. Si une ressource externe est indisponible, ce parcours reste à tester.

Skills similaires