write!() { /* proc-macro */ }Expand description
Writes formatted HTML to a buffer.
fhtml::write! works similar to std::write! with a few key differences:
- HTML can be written as-is without having to be inside a string literal.
- Expressions are written directly inside braces, compared to
std::write!, where they are passed as separate parameters.
Formatting specifiers are written after expressions, denoted by
a colon :, similar to how they are written in std::write!.
Values are not escaped implicitly, but are opt-in with an exclamation mark
! preceding any formatting specifiers:
{[expr]:![specifiers]}.
§Examples
§Simple usage
ⓘ
let mut buffer = String::new();
let _ = fhtml::write!(buffer, <div>"Hello, World!"</div>);
assert_eq!(buffer, "<div>Hello, World!</div>");§Escaping values
ⓘ
let mut buffer = String::new();
let user_input = "<b>Yay</b>";
let _ = fhtml::write!(buffer, <div>{user_input:!}</div>);
assert_eq!(buffer, "<div><b>Yay</b></div>");