pub struct Htmx {
pub is_htmx: bool,
pub boosted: bool,
pub history_restore_request: bool,
/* private fields */
}Expand description
Provides access to htmx request information and methods for setting htmx response headers.
The Htmx struct serves two main purposes:
- As an extractor, providing information about the current htmx request
- For managing htmx response headers
§Request Information
Access information about the current request:
use actix_web::{get, HttpResponse, Responder};
use actix_htmx::Htmx;
#[get("/")]
async fn handler(htmx: Htmx) -> impl Responder {
if htmx.is_htmx {
// This is an htmx request
println!("Target element: {}", htmx.target().unwrap_or_default());
println!("Trigger element: {}", htmx.trigger().unwrap_or_default());
}
// ...
HttpResponse::Ok()
}§Response Headers
Set htmx response headers for client-side behaviour:
use actix_web::{post, HttpResponse, Responder};
use actix_htmx::{Htmx, SwapType, TriggerPayload, TriggerType};
use serde_json::json;
#[post("/create")]
async fn create(htmx: Htmx) -> impl Responder {
// Trigger a client-side event
let payload = TriggerPayload::json(json!({ "id": 123 })).unwrap();
htmx.trigger_event(
"itemCreated",
Some(payload),
Some(TriggerType::Standard)
);
// Change how content is swapped
htmx.reswap(SwapType::OuterHtml);
// Redirect after the request
htmx.redirect("/items");
// ...
HttpResponse::Ok()
}Fields§
§is_htmx: boolTrue if the request was made by htmx (has the hx-request header)
boosted: boolTrue if the request was made by a boosted element (has the hx-boosted header)
history_restore_request: boolTrue if this is a history restore request (has the hx-history-restore-request header)
Implementations§
Source§impl Htmx
impl Htmx
pub fn new(req: &ServiceRequest) -> Htmx
Sourcepub fn current_url(&self) -> Option<String>
pub fn current_url(&self) -> Option<String>
Get the current URL from the hx-current-url header.
This header is sent by htmx and contains the current URL of the page.
Sourcepub fn prompt(&self) -> Option<String>
pub fn prompt(&self) -> Option<String>
Get the user’s response to an hx-prompt from the hx-prompt header.
This header contains the user’s input when an htmx request includes a prompt.
Sourcepub fn target(&self) -> Option<String>
pub fn target(&self) -> Option<String>
Get the ID of the target element from the hx-target header.
This header contains the ID of the element that will be updated with the response.
Sourcepub fn trigger(&self) -> Option<String>
pub fn trigger(&self) -> Option<String>
Get the ID of the element that triggered the request from the hx-trigger header.
This header contains the ID of the element that initiated the htmx request.
Sourcepub fn trigger_name(&self) -> Option<String>
pub fn trigger_name(&self) -> Option<String>
Get the name of the element that triggered the request from the hx-trigger-name header.
This header contains the name attribute of the element that initiated the htmx request.
Sourcepub fn trigger_event(
&self,
name: impl Into<String>,
payload: Option<TriggerPayload>,
trigger_type: Option<TriggerType>,
)
pub fn trigger_event( &self, name: impl Into<String>, payload: Option<TriggerPayload>, trigger_type: Option<TriggerType>, )
Trigger a custom JavaScript event on the client side.
This method allows you to trigger custom events that can be listened to with JavaScript. Events can include optional data and can be triggered at different points in the htmx lifecycle.
§Arguments
name- The name of the event to triggerpayload- Optional data to send with the event (typically JSON)trigger_type- When to trigger the event (defaults toStandard)
§Example
use actix_htmx::{Htmx, TriggerPayload, TriggerType};
fn handler(htmx: Htmx) {
// Simple event without data
htmx.trigger_event("item-deleted", None, None);
// Event with JSON data
let payload = TriggerPayload::json(serde_json::json!({
"message": "Success!",
"type": "info"
})).unwrap();
htmx.trigger_event(
"notification",
Some(payload),
Some(TriggerType::Standard)
);
}Sourcepub fn redirect(&self, path: impl Into<String>)
pub fn redirect(&self, path: impl Into<String>)
Redirect to a new page with a full page reload.
This sets the hx-redirect header, which causes htmx to perform a client-side redirect
to the specified URL with a full page reload.
Sourcepub fn redirect_with_swap(&self, path: impl Into<String>)
pub fn redirect_with_swap(&self, path: impl Into<String>)
Redirect to a new page using htmx (no full page reload).
This sets the hx-location header, which causes htmx to make a new request
to the specified URL and swap the response into the current page.
Sourcepub fn redirect_with_location(&self, location: HxLocation)
pub fn redirect_with_location(&self, location: HxLocation)
Redirect using a fully customized HX-Location object.
This lets you control additional behaviour like target selectors,
swap strategies, or custom values for the follow-up request.
Build the payload with HxLocation.
Sourcepub fn refresh(&self)
pub fn refresh(&self)
Refresh the current page.
This sets the hx-refresh header, which causes htmx to refresh the entire page.
Sourcepub fn push_url(&self, path: impl Into<String>)
pub fn push_url(&self, path: impl Into<String>)
Update the browser URL without causing a navigation.
This sets the hx-push-url header, which updates the browser’s address bar
and adds an entry to the browser history.
Sourcepub fn replace_url(&self, path: impl Into<String>)
pub fn replace_url(&self, path: impl Into<String>)
Replace the current URL in the browser history.
This sets the hx-replace-url header, which updates the browser’s address bar
without adding a new entry to the browser history.
Sourcepub fn reswap(&self, swap_type: SwapType)
pub fn reswap(&self, swap_type: SwapType)
Change how htmx swaps content into the target element.
This sets the hx-reswap header, which overrides the default swap behaviour
for this response.
Trait Implementations§
Source§impl FromRequest for Htmx
impl FromRequest for Htmx
Source§fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Self from request parts asynchronously.