feat: rip out mutations for templates

This commit is contained in:
Jonathan Kelley committed 2022-12-01 00:46:15 -05:00
1 parent 3b166c9edd
commit 85657d3906
33 files changed
+270 -314

No files matched your search

+2
View File
@@ -18,12 +18,14 @@ pub mod events;
pub mod geometry;
mod global_attributes;
pub mod input_data;
mod render_template;
#[cfg(feature = "wasm-bind")]
mod web_sys_bind;
pub use elements::*;
pub use events::*;
pub use global_attributes::*;
pub use render_template::*;
pub mod prelude {
pub use crate::events::*;
+41
View File
@@ -0,0 +1,41 @@
use dioxus_core::{Template, TemplateAttribute, TemplateNode};
use std::fmt::Write;
/// Render a template to an HTML string
///
/// Useful for sending over the wire. Can be used to with innerHtml to create templates with little work
pub fn render_template_to_html(template: &Template) -> String {
let mut out = String::new();
for root in template.roots {
render_template_node(root, &mut out).unwrap();
}
out
}
fn render_template_node(node: &TemplateNode, out: &mut String) -> std::fmt::Result {
match node {
TemplateNode::Element {
tag,
attrs,
children,
..
} => {
write!(out, "<{tag}")?;
for attr in *attrs {
if let TemplateAttribute::Static { name, value, .. } = attr {
write!(out, "{}=\"{}\"", name, value)?;
}
}
for child in *children {
render_template_node(child, out)?;
}
write!(out, "</{tag}>")?;
}
TemplateNode::Text(t) => write!(out, "{t}")?,
TemplateNode::Dynamic(_) => write!(out, "<pre hidden />")?,
TemplateNode::DynamicText(t) => write!(out, "<!-- --> {t} <!-- -->")?,
};
Ok(())
}