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.
- HTML tags must be well-formed and properly closed.
let user_info = v!;
v!
RsHtml derive macro
- Processes
.rs.htmltemplates from the defaultviewsdirectory. - 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), and pattern matching (@match). - 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()). - Supports raw output with
@rawblocks and server-side comments with@* ... *@.
<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);
}
@* This is a comment and will not appear in the output *@
use ;
Installation
To use RsHtml in your Rust project, run cargo add rshtml command or add it as a dependency in your Cargo.toml:
[]
= "0.5.0"
# For RsHtml derive macro:
# The default folder can be changed. This is the default setup:
[]
= { = "views", = false }
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve RsHtml.