3 releases (breaking)

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

#1290 in HTTP server

Download history 13/week @ 2024-07-22 25/week @ 2024-08-12 5/week @ 2024-08-19 9/week @ 2024-09-02 391/week @ 2024-09-09 2548/week @ 2024-09-16 3088/week @ 2024-09-23 3489/week @ 2024-09-30 2791/week @ 2024-10-07 5034/week @ 2024-10-14 4776/week @ 2024-10-21 6605/week @ 2024-10-28 7745/week @ 2024-11-04

24,515 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–30MB
~504K SLoC