fhtml
Simple and efficient macros for writing HTML in Rust
Overview
fhtml
Provides formatting macros for writing HTML without the annoyance of dealing with HTML inside string literals. A few highlights:
- simplicity: no complex templating syntax, just plain HTML with embedded expressions and format specifiers
- zero extra allocations:
fhtml
macros expand to theirstd
counterpart with no indirections or added allocations - compatibility: since
fhtml
is simply a wrapper overstd
macros, you can easily use idiomatic Rust, such as implementingfmt::Display
orfmt::Write
for creating components, or integrate with existing libraries and tools - safety:
fhtml
provides an easy way to escape values (escaping is NOT done implicitly)
Installation
In your Cargo.toml
:
[]
= "0.4"
Syntax
- HTML is typed as-is, unquoted:
!;
format
- Expressions are passed in using braces:
!;
format
- Text nodes are quoted:
!;
format
- Format specifiers are written after expressions:
!;
format
- Escaping is done by using an exclamation mark
!
as a format specifier:
!;
format
this being the only format specifier deviating from the std::fmt syntax
Usage
Writing to a buffer
let mut buffer = String new;
write!;
Escaping
let user_input = "<b>Dangerous input</b>";
format!; // "<div><b>Dangerous input</b></div>"
Components
Since fhtml
macros expands to their std
counterpart, you are free to create components however you prefer
Function components
let page = format! ;
Struct components
use fmt;
let products = format! ;
Format specifiers
!; // "<code>[1, 2, 3]</code>"
format!; // "<span>0b1010</span>"
format
Iterators
!
format