pub struct HtmxMiddleware;Expand description
A middleware for Actix Web that handles htmx specific headers and triggers.
This module provides middleware functionality for using htmx in your Actix Web application. It processes htmx headers and manages various types of triggers that can be used for client-side interactions.
HtmxMiddleware injects an Htmx struct into any route that it wraps. This
Htmx struct provides helper properties and methods that allow for your application
to easily interact with htmx.
§Example
use actix_web::{web, App, HttpServer, Responder, HttpResponse};
use actix_htmx::{Htmx, HtmxMiddleware};
#[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 {
HttpResponse::Ok().body(r##"
<!DOCTYPE html>
<html>
<head>
<title>htmx example</title>
<script src="https://unpkg.com/htmx.org@2.0.6"></script>
</head>
<body>
<div id="content">
This was not an htmx request! <a href="/" hx-get="/" hx-target="#content">Make it htmx!</a>
</div>
</body>
</html>
"##)
} else {
HttpResponse::Ok().body(r##"
<div id="content">
This was an htmx request! <a href="/">Let's go back to plain old HTML</a>
<div>
"##)
}
}The middleware automatically processes the following htmx headers:
HX-Trigger: For standard htmx triggersHX-Trigger-After-Settle: For triggers that fire after the settling phaseHX-Trigger-After-Swap: For triggers that fire after content swap
Trait Implementations§
Source§impl<S, B> Transform<S, ServiceRequest> for HtmxMiddlewarewhere
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
impl<S, B> Transform<S, ServiceRequest> for HtmxMiddlewarewhere
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
Source§type Response = ServiceResponse<B>
type Response = ServiceResponse<B>
Responses produced by the service.
Source§type Future = Ready<Result<<HtmxMiddleware as Transform<S, ServiceRequest>>::Transform, <HtmxMiddleware as Transform<S, ServiceRequest>>::InitError>>
type Future = Ready<Result<<HtmxMiddleware as Transform<S, ServiceRequest>>::Transform, <HtmxMiddleware as Transform<S, ServiceRequest>>::InitError>>
The future response value.
Source§fn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously
Auto Trait Implementations§
impl Freeze for HtmxMiddleware
impl RefUnwindSafe for HtmxMiddleware
impl Send for HtmxMiddleware
impl Sync for HtmxMiddleware
impl Unpin for HtmxMiddleware
impl UnwindSafe for HtmxMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more