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:
fhtmlmacros expand to theirstdcounterpart with no indirections or added allocations - compatibility: since
fhtmlis simply a wrapper overstdmacros, you can easily use idiomatic Rust, such as implementingfmt::Displayorfmt::Writefor creating components, or integrate with existing libraries and tools - safety:
fhtmlprovides 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 = Stringnew;
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
format!; // "<code>[1, 2, 3]</code>"
format!; // "<span>0b1010</span>"
Iterators
format!