actix_htmx/
lib.rs

1//! htmx middleware for Actix Web.
2//!
3//! `actix-htmx` provides a method of easily working with htmx in actix web applications.
4//! Wrap services with [`HtmxMiddleware`] to enable htmx support, and access the [`Htmx`]
5//! extractor in your handlers to get information about the current htmx state. Helper methods also
6//! exist to enable you to set htmx response headers, allowing easy triggering of htmx events from
7//! server side code.
8//!
9//! # Getting Started
10//! Register [`HtmxMiddleware`] on your `App`:
11//!
12//! ```no_run
13//! use actix_htmx::{Htmx, HtmxMiddleware, TriggerType};
14//! use actix_web::{web, App, HttpResponse, HttpServer, Responder};
15//!
16//! #[actix_web::main]
17//! async fn main() -> std::io::Result<()> {
18//!     HttpServer::new(|| {
19//!         App::new()
20//!             .wrap(HtmxMiddleware)
21//!             .service(web::resource("/").to(index))
22//!     })
23//!     .bind("0.0.0.0:8080")?
24//!     .run()
25//!     .await
26//! }
27//!
28//! async fn index(htmx: Htmx) -> impl Responder {
29//!     if htmx.is_htmx {
30//!         // build a partial view
31//!     } else {
32//!         // build a full view
33//!     }
34//!     htmx.trigger_event(
35//!         "my_event".to_string(),
36//!         Some(r#"{"level": "info", "message": "my event message!"}"#.to_string()),
37//!         Some(TriggerType::Standard)
38//!     );
39//!
40//!     HttpResponse::Ok().content_type("text/html").body(// render the view)
41//!
42//! }
43//! ```
44
45mod headers;
46mod htmx;
47mod middleware;
48
49pub use self::{
50    htmx::{Htmx, TriggerType},
51    middleware::HtmxMiddleware,
52};