plait
A fast, type-safe HTML templating library for Rust.
Plait provides compile-time HTML generation with a familiar, JSX-like syntax. Templates are checked at compile time and generate efficient code with minimal runtime overhead.
Quick Start
use html;
let name = "World";
let output = html!;
assert_eq!;
Macros
Plait provides four main macros:
html!- Creates anHtmlvalue from a templatecomponent!- Creates a reusable component that renders lazilyrender!- Renders content to an existingHtmlFormatterattrs!- Creates anAttributescollection
Template Syntax
Elements
Elements are written as name { children } for normal elements or name; for void elements:
use html;
let output = html!;
assert_eq!;
Attributes
Attributes support several value types:
use html;
let class_name = "container";
let maybe_id: = Some;
let is_disabled = true;
let output = html!;
assert_eq!;
Dynamic Content
Expressions in parentheses are escaped by default:
use html;
let user_input = "<script>alert('xss')</script>";
let output = html!;
// Content is safely escaped
assert!;
Use : raw to include pre-escaped content:
use html;
let trusted_html = "<strong>Bold</strong>";
let output = html!;
assert!;
Control Flow
Conditionals
use html;
let show = true;
let value: = Some;
let output = html!;
assert_eq!;
Loops
use html;
let items = vec!;
let output = html!;
assert_eq!;
Match Expressions
use html;
let status = Active;
let output = html!;
assert_eq!;
Custom Components
Use the component! macro to create reusable components as functions:
use ;
+ '_
// Use in templates
let output = html!;
assert_eq!;
For components with owned data, use (&value) to borrow:
use ;
For more control, implement the Render trait directly. See the Render documentation for details.
Safety
Plait automatically escapes dynamic content to prevent XSS vulnerabilities. The Html and PreEscaped types
represent content that is already safe and will not be escaped again.
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.