fhtml
fhtml is a crate that provides fast and straightforward macros for generating HTML content, similar to standard write! and format! macros, but tailored specifically for HTML output. This tool is ideal for applications requiring dynamic HTML generation.
Installation
Add fhtml to your project by including it in your Cargo.toml file:
[]
= "0.2"
Quick Start
Basic Formatting
Generate simple HTML strings with fhtml::format!. This macro behaves similarly to format!, but for HTML:
let output = format! ;
assert_eq!;
Writing to a Buffer
Directly write HTML to a buffer using fhtml::write!:
let mut output = Stringnew;
let _ = write! ;
assert_eq!;
Incorporating Expressions
Embed expressions within HTML content:
let output = format! ;
assert_eq!;
These macros expand to std::write!, supporting any compatible values.
Custom Components via Display Trait
Create reusable HTML components by implementing std::fmt::Display:
use fmt;
let products = format! ;
assert_eq!;
HTML Escaping
Note that fhtml does not perform HTML escaping implicitly. This means any HTML special characters included in strings will not be escaped automatically. To ensure your HTML content is secure from injection attacks, you can manually escape content using fhtml::escape:
let user_input = "<script>alert('xss');</script>";
let safe_output = format! ;
assert_eq!;
Always consider the context in which you are inserting user-generated content and use fhtml::escape appropriately to avoid XSS vulnerabilities.