Crate actix_htmx

Source
Expand description

§actix-htmx

actix-htmx provides a comprehensive solution for building dynamic web applications with htmx and Actix Web. It offers type-safe access to htmx request headers, easy response manipulation, and powerful event triggering capabilities.

§Features

  • Request Detection: Automatically detect htmx requests, boosted requests, and history restore requests
  • Header Access: Type-safe access to all htmx request headers (current URL, target, trigger, prompt, etc.)
  • Event Triggering: Trigger custom JavaScript events with optional data at different lifecycle stages
  • Response Control: Full control over htmx behaviour with response headers (redirect, refresh, swap, retarget, etc.)
  • Type Safety: Fully typed API leveraging Rust’s type system for correctness
  • Zero Configuration: Works out of the box with sensible defaults
  • Performance: Minimal overhead with efficient header processing

§Getting Started

Register HtmxMiddleware on your App and use the Htmx extractor in your handlers:

use actix_htmx::{Htmx, HtmxMiddleware};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .wrap(HtmxMiddleware)
            .route("/", web::get().to(index))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

async fn index(htmx: Htmx) -> impl Responder {
    if htmx.is_htmx {
        // This is an htmx request - return partial HTML
        HttpResponse::Ok().body("<div>Partial content for htmx</div>")
    } else {
        // Regular request - return full page
        HttpResponse::Ok().body("<html><body><div>Full page content</div></body></html>")
    }
}

Structs§

Htmx
Provides access to htmx request information and methods for setting htmx response headers.
HtmxMiddleware
A middleware for Actix Web that handles htmx specific headers and triggers.

Enums§

SwapType
Specifies how htmx should swap content into the target element.
TriggerType
Specifies when an htmx event should be triggered.