Expand description
§Basic usage
The following example demonstrates the basic usage of the library.
On top of any regular handler, you can add the [route] macro to create a typed route.
Any path- or query-parameters in the url will be type-checked at compile-time, and properly
extracted into the handler.
#![allow(unused)]
use axum::{
extract::State,
response::{Html, IntoResponse},
routing::get,
};
use axum_routing_htmx::{hx_get, hx_post, HtmxRouter};
#[hx_get("/title")]
async fn title_handler(State(state): State<String>) -> String {
format!("Hello from {state}!")
}
#[hx_post("/button/:id")]
async fn button_handler(id: u32) -> String {
format!("You clicked button #{id}!")
}
async fn index_handler() -> impl IntoResponse {
let title = title_handler();
let button = button_handler();
Html(format!(
"<html>
<head>
<meta charset=\"utf-8\" />
<script src=\"https://unpkg.com/htmx.org@2.0.2/dist/htmx.js\" crossorigin=\"anonymous\" />
</head>
<body>
<h1 {}=\"{}\" hx-trigger=\"load\">Loading...</h1>
<button {}=\"{}\" id=\"button-1\">Click me!</button>
</body>
</html>",
title.htmx_method(),
title.htmx_path(),
button.htmx_method(),
button.htmx_path(1),
))
}
fn main() {
let router: axum::Router = axum::Router::new()
.htmx_route(title_handler())
.with_state("axum-routing-htmx".to_string())
.htmx_route(button_handler())
.route("/", get(index_handler));
}Some valid url’s as get-methods are:
/item/1?amount=2&offset=3/item/1?amount=2/item/1?offset=3/item/500
Enums§
- Htmx
Method - The HTTP verbs supported by HTMX.
Traits§
- Htmx
Handler - The trait expected by the Router to add HTMX routes.
- Htmx
Router - A trait that allows typed routes, created with the
hx_macros to be added to an axum router.
Attribute Macros§
- hx_
delete - A macro that generates HTMX-compatible statically-typed Delete routes for axum handlers.
- hx_get
- A macro that generates HTMX-compatible statically-typed Get routes for axum handlers.
- hx_
patch - A macro that generates HTMX-compatible statically-typed Patch routes for axum handlers.
- hx_post
- A macro that generates HTMX-compatible statically-typed Post routes for axum handlers.
- hx_put
- A macro that generates HTMX-compatible statically-typed Put routes for axum handlers.