6 releases (breaking)

0.5.1 Apr 5, 2023
0.5.0 Feb 20, 2023
0.4.0 Jun 20, 2022
0.3.0 Sep 8, 2021
0.1.0 Apr 16, 2021

#70 in Template engine

Download history 42/week @ 2023-12-13 45/week @ 2023-12-20 42/week @ 2023-12-27 57/week @ 2024-01-03 71/week @ 2024-01-10 63/week @ 2024-01-17 44/week @ 2024-01-24 47/week @ 2024-01-31 80/week @ 2024-02-07 139/week @ 2024-02-14 124/week @ 2024-02-21 167/week @ 2024-02-28 149/week @ 2024-03-06 133/week @ 2024-03-13 169/week @ 2024-03-20 64/week @ 2024-03-27

533 downloads per month
Used in 3 crates

Unlicense

37KB
632 lines

html-builder

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>
  • It automatically closes your tags for you. This guarantees the output will be well-formed. It also makes your code nicer - closing tags by hand is a drag.
  • It also provides helper methods for all the valid HTML5 tags. You don't have to use these, but if you do it makes sure you don't typo a tag name.

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.

Dependencies

~145KB