Skip to content

A less dom and more template approach #34

@daaku

Description

@daaku

Currently the way the rsx! macro parses and outputs something akin to a psuedo-dom style set of element and attributes is similar to what React does. But the current use case of this crate is server side rendering (unless you have plans for this with wasm), a dom like structure is an implementation detail. With this in mind, it would be more performant to pass thru most of the template as static bytes to write calls, and splice in dynamic content where we find it.

As an example, for this component:

#[component]
fn Container<'kind, Children: Render>(
    kind: &'kind str,
    children: Children,
) {
    rsx! {
        <div class={kind}>
          {children}
        </div>
    }
}

Old output:

let result = {
    let Container { kind, children } = self;
    {
        ::render::SimpleElement {
            tag_name: "div",
            attributes: {
                let mut hm = std::collections::HashMap::<&str, &str>::new();
                hm.insert("class", kind);
                Some(hm)
            },
            contents: Some(children),
        }
    }
};
::render::Render::render_into(result, w)

New output:

let Container { kind, children } = self;
write!(w, b"<div class=")?;
::render::Render::render_into(kind, w)?;
write!(w, b">")?;
::render::Render::render_into(children, w)?;
write!(w, b"</div>")?;
Ok(())

I think there are other things to consider with this change, like automatic quoting of attribute, punning (which I independently am unsure of) etc. I'll figure that out as I make progress. I'm going to experiment with this idea and see how far I can take it without running into unforeseen issues.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions