macro_rules! fragment {
() => { ... };
(move { $($input:tt)* }) => { ... };
({ $($input:tt)* }) => { ... };
}
Expand description
Creates a Fragment
which can be inserted into a DomBuilder
.
A fragment is a collection of children, you can use all of the methods in FragmentBuilder
:
let x = fragment!({
.text("foo")
.child(html!("div", { ... }))
.children_signal_vec(...)
});
You can then insert the fragment into a DomBuilder
:
html!("div", {
.fragment(&x)
})
The fragment is inlined, so it is exactly the same as if you had written this, there is no performance cost:
html!("div", {
.text("foo")
.child(html!("div", ...))
.children_signal_vec(...)
})
The same fragment can be inserted multiple times, and it can be
inserted into multiple different DomBuilder
.
Fragments are very useful for passing children into another component:
Foo::render(&state.foo, fragment!({
.text("Hello!")
}))
There are three syntaxes for fragment:
-
fragment!()
creates an empty fragment. -
fragment!({ ... })
creates a normal fragment. -
fragment!(move { ... })
creates a fragment whichmove
s the outer variables into the fragment, just like a closure.
When returning a fragment from a function, you will usually need to use the move
syntax:
fn my_fragment() -> impl Fragment {
let x = some_string();
fragment!(move {
.text(&x)
})
}