#builder #html #escaping #attributes #elements #attr #helper

another-html-builder

Yet another html builder, focused on being a helper for creating elements, escaping attributes, escaping text, but not caring if the html structure is valid. It's up to the developer to test that.

3 releases

new 0.1.2 Nov 19, 2024
0.1.1 Nov 13, 2024
0.1.0 Nov 11, 2024

#2 in #attr

Download history 254/week @ 2024-11-11

254 downloads per month

MIT/Apache

26KB
492 lines

Another HTML builder

The goal of this builder is to be simple, to only rely on the standard library, to avoid copying values and write directly to a buffer. There is no lock involved, the ownership of the buffer is the only thing that will avoid race conditions.

Example

const STYLE_PATH: &str = "/assets/style-0.0.1.css";

// you can define components like this
fn render_head(buf: Buffer<String, Body<'_>>) -> Buffer<String, Body<'_>> {
    buf.node("head").content(|buf| {
        buf.node("meta")
            .attr(("charset", "utf-8"))
            .close()
            .node("meta")
            .attr(("name", "viewport"))
            .attr(("content", "width=device-width, initial-scale=1"))
            .close()
            .node("title")
            .content(|buf| buf.text("This is a wonderful title"))
            .node("link")
            .attr(("rel", "stylesheet"))
            .attr(("href", STYLE_PATH))
            .close()
    })
}

let html = another_html_builder::Buffer::default()
    .doctype()
    .node("html")
    .attr(("lang", "en"))
    .content(|buf| {
        let buf = render_head(buf);
        buf.node("body").content(|buf| {
            buf.node("div")
                .attr(("class", "card"))
                .content(|buf| {
                    buf.node("div")
                        .attr(("class", "card-header text-center")) // with string attributes
                        .attr(("attr-index", 42)) // with a number
                        .attr(("attr-visible", true)) // with a boolean
                        .content(|buf| buf.text("Show me what you got!"))
                        .node("div")
                        .attr(("class", "card-body"))
                        .content(|buf| buf.text("This will be encoded < and this will be escaped \""))
                })
        })
    })
    .into_inner();

No runtime deps