3 releases (breaking)

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

#1314 in HTTP server

26 downloads per month

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–33MB
~553K SLoC