9 releases

0.2.6 Mar 14, 2024
0.2.5 Jan 18, 2024
0.2.4 Aug 18, 2023
0.1.1 Aug 6, 2023

#7 in #glue

Download history 6/week @ 2023-12-29 2/week @ 2024-01-05 4/week @ 2024-01-12 3/week @ 2024-01-19 9/week @ 2024-02-16 31/week @ 2024-02-23 85/week @ 2024-03-01 111/week @ 2024-03-08 70/week @ 2024-03-15 16/week @ 2024-03-22 22/week @ 2024-03-29 10/week @ 2024-04-05

211 downloads per month

MIT license

46KB
561 lines

rstml-component-axum: Integration with rstml-component for Axum

rstml-component-axum is a crate designed to streamline the usage of rstml-component within Axum projects. This crate provides glue code and helpers to make it easier to create dynamic HTML content using rstml-component within your Axum applications.

Features

  • Simplified Integration: Seamlessly integrate rstml-component into your Axum handlers for efficient HTML generation.

  • Optimized for Axum: Enjoy the benefits of both rstml-component and Axum for building high-performance server applications.

Installation

To use rstml-component-axum in your Axum project, add it as a dependency in your Cargo.toml:

[dependencies]
rstml-component-axum = "0.1.0"

Usage

Here's a basic example demonstrating how to use rstml-component-axum to integrate rstml-component with an Axum handler:

use axum::{response::IntoResponse, routing::get, Router};
use rstml_component::{move_html, write_html, For, HtmlComponent, HtmlContent};
use rstml_component_axum::HtmlContentAxiosExt;
use std::net::SocketAddr;

#[derive(HtmlComponent)]
struct Book {
	title: &'static str,
	author: &'static str,
}

impl Book {
	fn new(title: &'static str, author: &'static str) -> Self {
		Self { title, author }
	}
}

impl HtmlContent for Book {
	fn fmt(self, formatter: &mut rstml_component::HtmlFormatter) -> std::fmt::Result {
		write_html!(formatter,
			<div>
				<h1>{self.title}</h1>
				<h2>"("{self.author}")"</h2>
			</div>
		)
	}
}

// Your Axum handler
async fn index() -> impl IntoResponse {
	let books = [
		("Moby Dick", "Herman Melville"),
		("Lord of the Rings", "John Ronald Reuel Tolkien"),
	];

	move_html!(
		<div class="books">
			<For items={books}>
				{ |f, book| Book::new(book.0, book.1).fmt(f) }
			</For>
		</div>
	)
	.into_html()
}

#[tokio::main]
async fn main() {
	let app = Router::new().route("/", get(index));

	let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
	println!("listening on {}", addr);

	axum::Server::bind(&addr)
		.serve(app.into_make_service())
		.await
		.unwrap();
}

For a more detailed walkthrough and additional examples, refer to the documentation for rstml-component-axum.

License

This project is licensed under the MIT License.

Dependencies

~8–17MB
~189K SLoC