1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 { text: t } => write!(out, "{t}")?,
        TemplateNode::Dynamic { id: _ } => write!(out, "<pre hidden />")?,
        TemplateNode::DynamicText { id: t } => write!(out, "<!-- --> {t} <!-- -->")?,
    };
    Ok(())
}