plait
A lightweight, type-safe HTML templating library for Rust.
Plait provides a macro-based DSL for writing HTML directly in Rust code with compile-time validation and automatic escaping. It's designed for building server-side rendered HTML with minimal runtime overhead.
Quick Start
use ;
let name = "World";
let html = render;
assert_eq!;
The html! Macro
The html! macro provides a concise syntax for writing HTML:
use ;
let html = render;
Expressions
Use parentheses to embed expressions. Content is automatically HTML-escaped:
use ;
let user_input = "<script>alert('xss')</script>";
let html = render;
assert_eq!;
For raw, unescaped content, prefix with #:
use ;
let trusted_html = "<strong>Bold</strong>";
let html = render;
assert_eq!;
Attributes
Attributes support several forms:
use ;
let class = Some;
let disabled = true;
let html = render;
Control Flow
Standard Rust control flow works naturally:
use ;
let show = true;
let items = vec!;
let variant = "primary";
let user = Some;
let html = render;
Components
Create reusable components using the component! macro:
use ;
component!
let html = render;
assert_eq!;
Inside components, #attrs spreads additional HTML attributes and #children renders the component's children.
Passing HTML as props
Components can accept html! fragments as props using the ToHtml trait:
use ;
component!
let html = render;
assert_eq!;
URL Safety
URL attributes (href, src, action, etc.) are automatically validated. Dangerous schemes like javascript:
are stripped:
use ;
let html = render;
assert_eq!; // href removed
Use #(...) for raw URLs when you trust the source.
Performance
For better performance when output size is predictable, use render_with_capacity to pre-allocate the buffer:
use ;
let html = render_with_capacity;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.