Function insert_components

Source
pub fn insert_components(contents: String, component: Component<'_>) -> String
Expand description

Takes a String and swaps the placeholder text {COMPONENT} for the HTML String built using the provided struct component

ยงExample

use html_compile::compile::*;
use html_compile::types::*;

let contents = String::from("<html lang=\"en\"><head><title>Test Data</title></head><body>{COMPONENT}</body></html>");

let input_component = Component {
    tag: "section",
    meta: Some(vec![
        Attribute {
            label: "style",
            value: "border: 1px solid black; padding: 10px;",
        },
        Attribute {
            label: "class",
            value: "Example",
        },
    ]),
    child: Child::ComponentVec(vec![
        Box::new(Component {
            tag: "h2",
            meta: None,
            child: Child::Text("Text of a Heading"),
        }),
        Box::new(Component {
            tag: "p",
            meta: None,
            child: Child::Text("Text of a Paragraph"),
        }),
    ]),
};

let output = insert_components(contents, input_component);  

assert_eq!(output, String::from("<html lang=\"en\"><head><title>Test Data</title></head><body><section style=\"border: 1px solid black; padding: 10px;\" class=\"Example\"><h2>Text of a Heading</h2><p>Text of a Paragraph</p></section></body></html>"));

In a browser the string will display as follows:

Test Data

Text of a Heading

Text of a Paragraph