schell / mogwai

The minimalist, obvious, graphical, web application interface
429 stars 26 forks source link

Creating a View/ViewBuilder from a Vec #85

Closed paulalesius closed 2 years ago

paulalesius commented 2 years ago

Hi

Is it possible to create a list of elements?

impl From<Vec<HtmlElement>> for ViewBuilder<HtmlElement> {
    fn from(vec: Vec<HtmlElement>) -> Self {
        builder! {
            // How do you construct a ViewBuilder or View from a Vec of elements?
        }
    }
}

I've found no example, and none of the code I've looked at is using a Vec of items. I'd want to pre-create a list of items so that they are displayed at once, instead of sending them one by one.

Thank you for a great framework!

bryanjswift commented 2 years ago

I think this is related to https://github.com/schell/mogwai/issues/82; the discussion there may be useful. I think the short version is "not right now but probably in the next version"

paulalesius commented 2 years ago

Thank you for the quick reply! I just read that but did not understand what it meant by "fragment", sorry. Closing! 👍

schell commented 2 years ago

Yes, the next release will allow adding a vector of ViewBuilder like so:

        let vs: Vec<ViewBuilder<Dom>> = builder! {
            <div>"hello"</div>
            <div>"hola"</div>
            <div>"kia ora"</div>
        };

        let s: ViewBuilder<Dom> = builder! {
            <section>{vs}</section>
        };
        let view: View<Dom> = s.try_into().unwrap();
        assert_eq!(
            String::from(view).as_str(),
            "<section><div>hello</div> <div>hola</div> <div>kia ora</div></section>"
        );