Expand description
Just a simple toolkit for writing html.
This provides the basic functions needed to write basic html or create components to build a rich and complete UI.
§Example
In this example, we create a custom attribute and also a custom Head
element.
use another_html_builder::{AttributeValue, Body, Buffer};
enum Lang {
En,
Fr,
}
impl AttributeValue for Lang {
fn render(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::En => "en",
Self::Fr => "fr",
})
}
}
struct Head {
title: &'static str,
}
impl Default for Head {
fn default() -> Self {
Self {
title: "Hello world!",
}
}
}
impl Head {
fn render<'a, W: std::fmt::Write>(&self, buf: Buffer<W, Body<'a>>) -> Buffer<W, Body<'a>> {
buf.node("head")
.content(|buf| buf.node("title").content(|buf| buf.text(self.title)))
}
}
let head = Head::default();
let html = Buffer::default()
.doctype()
.node("html")
.attr(("lang", Lang::Fr))
.content(|buf| head.render(buf))
.into_inner();
assert_eq!(
html,
"<!DOCTYPE html><html lang=\"fr\"><head><title>Hello world!</title></head></html>"
);
Structs§
- Wrapper used for displaying attributes in elements
- Wrapper arround a writer element.
- Representation of an element
Enums§
- Representation of the inside of an element or the root level.
Traits§
- Represents an element attribute name.
- Represents an element attribute value.
Functions§
- Helper to write
&str
attributes to a Write and automatically escape - Helper to write
&str
content to a Write and automatically escape - write_
escaped_ attribute_ str Deprecated Helper to write&str
attributes to a Write and automatically escape - write_
escaped_ content_ str Deprecated Helper to write&str
content to a Write and automatically escape