3 releases (breaking)

0.3.0 Nov 13, 2023
0.2.0 Oct 23, 2023
0.1.0 Aug 17, 2023

#1325 in HTTP server

Download history 5/week @ 2024-08-18 1/week @ 2024-08-25 5/week @ 2024-09-01 180/week @ 2024-09-08 2575/week @ 2024-09-15 3227/week @ 2024-09-22 3277/week @ 2024-09-29 2858/week @ 2024-10-06 4743/week @ 2024-10-13 4242/week @ 2024-10-20 7216/week @ 2024-10-27 7712/week @ 2024-11-03 5695/week @ 2024-11-10 11343/week @ 2024-11-17 11439/week @ 2024-11-24 5922/week @ 2024-12-01

35,319 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

22KB
442 lines

actix-htmx

Middleware to make working with htmx and Actix Web easier.

crates.io Apache 2.0 or MIT licensed

actix-htmx allows you to easily interact with htmx request and response headers from within an Actix Web application.


lib.rs:

htmx middleware for Actix Web.

actix-htmx provides a method of easily working with htmx in actix web applications. Wrap services with HtmxMiddleware to enable htmx support, and access the Htmx extractor in your handlers to get information about the current htmx state. Helper methods also exist to enable you to set htmx response headers, allowing easy triggering of htmx events from server side code.

Getting Started

Register HtmxMiddleware on your App:

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

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

async fn index(htmx: Htmx) -> impl Responder {
    if htmx.is_htmx {
        // build a partial view
    } else {
        // build a full view
    }
    htmx.trigger_event(
        "my_event".to_string(),
        Some(r#"{"level": "info", "message": "my event message!"}"#.to_string()),
        Some(TriggerType::Standard)
    );

    HttpResponse::Ok().content_type("text/html").body(// render the view)

}

Dependencies

~18–29MB
~502K SLoC