Macro laby::frag

source ·
frag!() { /* proc-macro */ }
Expand description

Wraps multiple values implementing Render into one.

This macro is similar to React fragments which wrap multiple nodes into one. It is useful when passing multiple values to a function that accepts only one value, or when returning multiple values as one return value.

All wrapped values will be rendered sequentially in the order of arguments without delimiters.

Example

The following example passes multiple nodes to a function that accepts only one node, by wrapping the arguments in frag!. By using fragments, intermediary container elements like div or span, which changes the semantics of the markup, can be avoided.

This example passes multiple nodes to a function which takes only one value.

fn component(node: impl Render) -> impl Render {
    ul!(node)
}

let s = render!(component(frag!(
    li!("one"),
    li!("two"),
)));

assert_eq!(s, "<ul><li>one</li><li>two</li></ul>");

This example returns multiple nodes from a function which returns only one value.

fn component() -> impl Render {
    frag!(
        li!("one"),
        li!("two"),
    )
}

let s = render!(ul!(component()));
assert_eq!(s, "<ul><li>one</li><li>two</li></ul>");