Rshtml
RsHtml Template
Install / Use
/learn @rshtml/RshtmlREADME
RsHtml
RsHtml is a compile-time, type-safe, lightweight, and flexible template engine for Rust. It aims to seamlessly integrate Rust code with HTML content. It allows developers to write dynamic templates that embed Rust expressions and makes it easier to generate HTML content programmatically. It generates efficient Rust code for template rendering at compile time.

Documentation and Editor Support
- See the Documentation for a full list of features and the Editor Support section for editor integration details. A minimal working website example is also available in the
rshtml_testcrate.
v! macro
- Allows writing Rust blocks inside HTML (
<div>{ code() }</div>) and embedding the resulting expressions into the template. - Generates a type that implements the View trait and can render types that implement either the View or Display trait.
let user_info = v!(<p>name: {user.name}</p>);
v! {
<div class="user-info"> {user_info} </div>
}
fn cards(cards: &[Card]) -> impl View {
let mut card_views = Vec::new();
for card in cards {
card_views.push(v!(<div class="card">{&card.title}</div>));
}
v! {
<div>
{ card_views }
</div>
}
}
impl View for Home {
fn render(&self, out: &mut dyn Write) -> Result {
v!(<p>Home Page {&self.title}</p>).render(out)
}
}
View derive macro
Processes .rs.html templates from the views directory or the file in the specified folder and implements the View trait.
- Embeds Rust expressions and blocks directly into HTML templates using the
@prefix or HTML-like component syntax (e.g.,<Component/>). - Supports conditional rendering (
@if,@else), loops (@for,@while). - Supports Rust code blocks (
@{}), various Rust expression syntaxes (e.g.,@expression,@(expression), and a broad range of other Rust syntax. - Provides helper functions (e.g.,
@time()).
<h1>Welcome to RsHtml</h1>
@use "Component.rs.html" as Component
<Component title="home" is_ok=true>
<p>child content</p>
</Component>
@if self.is_logged_in {
<p>Hello, @self.username!</p>
} else {
<p>Please log in to continue.</p>
}
<ul>
@for item in self.items {
<li>@item</li>
}
</ul>
@{
let x = 42;
let y = x * 2;
println!("Debug: x = {}, y = {}", x, y);
}
use rshtml::View;
#[derive(View)]
// #[view(path = "views/home.rs.html", extract)]
struct HomePage { // Looks for views/home.rs.html.
title: String,
}
fn main() {
let homepage = HomePage {
title: "Home Page".to_string()
};
let mut out = String::with_capacity(homepage.text_size());
homepage.render(&mut out).unwrap();
print!("{}", out);
}
Installation
To use RsHtml in your Rust project, run cargo add rshtml command or add it as a dependency in your Cargo.toml:
[dependencies]
rshtml = "0.6.1"
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve RsHtml.
