HTML renderer for lignin VDOM Nodes.
This crate is primarily for static and server-side rendering.
For client-side use, see lignin-dom.
Please use cargo-edit to always add the latest version of this library:
cargo add lignin-html
use lignin::{Node, Element, ElementCreationOptions};
use lignin_html::render_document;
let mut document = String::new();
render_document(
&Node::HtmlElement {
element: &Element {
// `lignin-html` is case-preserving but insensitive.
// `lignin-dom` slightly prefers all-caps, as may other DOM renderers.¹
name: "DIV",
creation_options: ElementCreationOptions::new(), // `const fn` builder pattern.
attributes: &[],
content: Node::Multi(&[
"Hello! ".into(),
Node::Comment {
// `lignin-html` escapes or substitutes where necessary and unobtrusive.
comment: "--> Be mindful of HTML pitfalls. <!--",
dom_binding: None,
}
]),
event_bindings: &[],
},
dom_binding: None, // Bindings are inactive with `lignin-html`.
}
.prefer_thread_safe(),
&mut document,
1000, // depth_limit
).unwrap(); // Invalid content that can't be escaped is rejected.
assert_eq!(
document,
"<!DOCTYPE html><DIV>Hello! <!--==> Be mindful of HTML pitfalls. <!==--></DIV>",
);
¹ See Element.tagName (MDN). This avoids case-insensitive comparisons, but can cause elements to be recreated during hydration if the reported tagName doesn't quite match the generated name
.
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING for more information.
lignin-html
strictly follows Semantic Versioning 2.0.0 with the following exceptions:
- The minor version will not reset to 0 on major version changes (except for v1).
Consider it the global feature level. - The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
Consider it the global patch level.
This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.
Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.
Note that dependencies of this crate may have a more lenient MSRV policy!
Please use cargo +nightly update -Z minimal-versions
in your automation if you don't generate Cargo.lock manually (or as necessary) and require support for a compiler older than current stable.