rstml: variant of `html-to-string-macro` which returns something that `impl ToString` (return the nodes themselves)

typed_html does this, and i believe it allows them to do cool things like having iterators in the macro. currently, i believe you would have to use the html! macro to create strings in the loop then join them at the end, which seems kinda jank.

would this be possible?

edit: written by myself: https://github.com/vidhanio/html-node

About this issue

Most upvoted comments

@infogulch - yep! the # flag enables pretty-printing, whereas without it, it all prints on one line (much like how Debug with #? works)

@infogulch… a few hours later, i’ve cooked up something AMAZING

#[derive(Debug, Clone)]
struct Location {
    x: i32,
    y: i32,
}

impl Display for Location {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{},{}", self.x, self.y)
    }
}

typed::element! {
    MyElement("my-element") {
        location: Location,
    }
}

typed::attributes! {
    [Hx] {
        hx_boost: bool,
    }
}
            // vvvvvvvvvvvvvvvvvvvvvvvvvvvvv attribute extension list
typed::html! { (hx: Hx, extras)
    ...
}

image image image

ident: Type in the “extensions” list will use the Type as an extension set of typed attributes, and those with just ident will be added to the list of “other” attributes like data-* and aria-*

and you can have as many extensions as you want! image

and then, you can create your own decl macro for your crate that wraps typed::html! to add your custom attribute extensions for all macros.

macro_rules! html_hx {
    ($($tt:tt)*) => {::html_node::typed::html!((hx: $crate::Hx) $($tt)*)};
}

@vldm - maybe there could be a section called “powered by rstml” in the readme that this could be put under? then maybe you could create an example which uses html-node in this repository as well.

@vidhanio Hi, awesome work! I thought of adding more complex example of rstml usage (maybe even to fork typed-html). But haven’t a lot of spare time to do it. Originally i have created rs-tml organisation to keep tools/examples in one place, and to be able to manage ownership of rstml repository.

So if you want, we can move html-node to rs-tml organisation. Or/and we can add link to it in README section of rstml.

I would like to keep original html-to-string macro in examples and not replace it, since it is a minimal usage variant and doesn’t involve any additional runtime.

alright @infogulch, i’ve got this test case passing! i’ll push my code and update you when it’s ready to take a look at.

use html_node::{html, text};

#[test]
fn works() {
    let html = html!(
        <div>
            <ul>
                { (0..5).map(|i| html!(<li>{ text!("Item {i}") }</li>)) }
            </ul>
        </div>
    );

    let expected = concat!(
        "<div>",
        "<ul>",
        "<li>Item 0</li>",
        "<li>Item 1</li>",
        "<li>Item 2</li>",
        "<li>Item 3</li>",
        "<li>Item 4</li>",
        "</ul>",
        "</div>",
    );

    assert_eq!(html.to_string(), expected);
}