[−][src]Crate build_html
This library is designed to provide a simple way to generate HTML documents from within Rust
code. To generate documents, this library uses the builder pattern; calls to add elements are
repeatedly chained together to dynamically build up an HTML document. The document is then
flushed to an HTML string which can be used elsewhere in your program with to_html_string()
Using
Everything you need to use this crate has been exported from the crate root. This means that
you can get easy access to every element using the import: use build_html::*
.
If compatibility is important, or you don't need access to every element, you can also import
elements piecemeal and prefix types with the package name. Note that the traits HTML
and
HtmlContainer
must be in scope for the library to be useful:
use build_html::{self, Html, HtmlContainer}; let page = build_html::HtmlPage::new() .add_paragraph("Some Text") .to_html_string();
Once the package is imported, the HtmlPage
struct will provide the base for most use cases.
This struct provides the boilerplate for an HTML5 page, and allows content to be added to it
using chained methods.
Example
This example uses the maplit
crate to create HashMap
literals. If your use case requires adding additional attributes, I would recommend this crate
for its simplicity.
use build_html::*; use maplit::hashmap; let html: String = HtmlPage::new() .add_title("My Page") .add_header(1, "Main Content:") .add_container( Container::new(ContainerType::Article) .with_attributes(hashmap! {"id" => "article1"}) .add_header_attr(2, "Hello, World", hashmap! {"id" => "article-head"}) .add_paragraph("This is a simple HTML demo") ) .to_html_string();
produces a string equivalent to:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Main Content:</h1>
<article id="article1">
<h2 id="article-head">Hello World</h2>
<p>This is a simple HTML demo</p>
</article>
</body>
</html>
Structs
Container | A container for HTML elements. |
HtmlPage | This struct represents an entire page of HTML which can built up by chaining addition methods. |
Table | Represents an HTML |
Enums
ContainerType | The different types of Html Containers that can be added to the page |
Traits
Html | An element that can be converted to an HTML string |
HtmlContainer | An HTML element that can contain other HTML elements |