pub trait Component {
// Required method
fn render(
self,
f: &mut HtmlFormatter<'_>,
attrs: impl FnOnce(&mut HtmlFormatter<'_>),
children: impl FnOnce(&mut HtmlFormatter<'_>),
);
}Expand description
A trait for creating reusable HTML components.
Components encapsulate HTML structure and behavior, allowing you to define reusable UI elements with their own properties while still accepting additional attributes and children from the call site.
§Usage
Use the component! macro to define components:
use plait::{component, html, render};
component! {
pub fn Button<'a>(class: &'a str, disabled: bool) {
button(class: format_args!("btn {}", class), disabled?: disabled, #attrs) {
#children
}
}
}
let html = render(html! {
@Button(class: "btn-primary", disabled: false; id: "button-id") {
"Click me"
}
});
assert_eq!(html, r#"<button class="btn btn-primary" id="button-id">Click me</button>"#);The component! macro generates a struct and implements this trait automatically. See
component! for the full syntax reference.
Required Methods§
Sourcefn render(
self,
f: &mut HtmlFormatter<'_>,
attrs: impl FnOnce(&mut HtmlFormatter<'_>),
children: impl FnOnce(&mut HtmlFormatter<'_>),
)
fn render( self, f: &mut HtmlFormatter<'_>, attrs: impl FnOnce(&mut HtmlFormatter<'_>), children: impl FnOnce(&mut HtmlFormatter<'_>), )
Renders the component to the given HTML formatter.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.