Crates.io | html-builder |
lib.rs | html-builder |
version | 0.5.1 |
source | src |
created_at | 2021-04-16 01:33:15.32039 |
updated_at | 2023-04-05 02:32:54.850675 |
description | Flexible and convenient HTML generation |
homepage | |
repository | https://github.com/asayers/html-builder |
max_upload_size | |
id | 385151 |
size | 44,266 |
This crate helps you generate HTML.
let mut buf = Buffer::new(); // Contents added to buffer by each statement:
let mut html = buf.html().attr("lang='en'"); // <html lang='en'>
writeln!(html.head().title(), "Title!")?; // <head><title>Title!
writeln!(html.body().h1(), "Header!")?; // </title></head><body><h1>Header!
buf.finish(); // </h1></body></html>
IMO it strikes a good balance of safety to simplicity/flexibility.
It's by no means built for performance, but nevertheless the performance
is OK I think. The benchmark generates a small (20-line) HTML document.
On my laptop it runs in 2µs. Run cargo bench
to check for yourself.
There are high quality templating libraries out there, such as askama, but personally I don't like templates. typed-html is closer to what I want, and it aims for a high level of type safety (eg. it forbids div-level elements inside span-level elements). This is great in theory, but I found working with it was too complex. This crate is much simpler by comparison, at the expense of not catching as many bugs. The design is inspired by Haskell's HTML combinator libraries such as blaze-html and lucid.