9 releases

new 0.2.4 May 16, 2024
0.2.3 Mar 14, 2024
0.2.2 Jan 18, 2024
0.2.1 Aug 18, 2023
0.1.3 Aug 17, 2023

#110 in Template engine

Download history 5/week @ 2024-01-19 3/week @ 2024-01-26 2/week @ 2024-02-02 12/week @ 2024-02-16 32/week @ 2024-02-23 53/week @ 2024-03-01 114/week @ 2024-03-08 78/week @ 2024-03-15 29/week @ 2024-03-22 28/week @ 2024-03-29 22/week @ 2024-04-05 16/week @ 2024-04-12 11/week @ 2024-04-19 15/week @ 2024-04-26 7/week @ 2024-05-03

54 downloads per month
Used in rstml-component-axum

MIT license

37KB
628 lines

rstml-component: HTML Component Library for Rust

rstml-component is a Rust library that empowers developers to create dynamic HTML components efficiently. With this library, you can define HTML components using Rust structs and easily generate HTML on-the-fly, making it especially useful for server-side applications that need to produce HTML content.

Features

  • Declarative Component Definition: Define HTML components using Rust structs, making your code more organized and maintainable.

  • Effortless HTML Generation: Generate HTML content dynamically by leveraging the power of Rust's expressive syntax.

  • Designed for Server-Side Applications: Perfectly suited for server applications that need to generate HTML content on-the-fly.

  • Template Reusability: Create reusable templates by structuring components, enhancing code reusability across your project.

Installation

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

[dependencies]
rstml-component = "0.2.1"

Usage

Here's a quick example to demonstrate how easy it is to define and use HTML components using rstml-component:

use rstml_component::{HtmlComponent, HtmlContent, HtmlFormatter};

#[derive(HtmlComponent)]
struct Page<T>
where
		T: Into<String>,
{
	title: T,
	heading: String,
}

impl<T> HtmlContent for Page<T>
where
		T: Into<String>,
{
	fn fmt(self, formatter: &mut HtmlFormatter) -> std::fmt::Result {
		write_html!(formatter,
			<Template title=self.title attribute="world">
				<h1>{self.heading}</h1>
				<p>"This is a test"</p>
			</Template>
		)
	}
}

// Example Axum handler - requires rstml-component-axum
async fn index() -> impl IntoResponse {
	Page {
		title: "My Title",
		heading: "Page Heading",
	}
	.into_html()
}

For more detailed information and examples, please refer to our Documentation.

License

This project is licensed under the MIT License.

Dependencies

~2–8MB
~61K SLoC