Skip to main content
Version: Next

Conditional rendering

If blocks

To conditionally render some markup, we wrap it in an if block:

use yew::prelude::*;

html! {
if true {
<p>{ "True case" }</p>
}
};

Match blocks

match expressions work directly in html!, following the same pattern as if/else:

use yew::prelude::*;

let value: Option<String> = Some("hello".into());

html! {
match value {
Some(text) => <p>{text}</p>,
None => <p>{"Nothing here"}</p>,
}
};

Arms with a single element can omit braces. Arms with multiple children or let bindings require braces.

match supports all standard Rust patterns including OR-patterns (A | B), destructuring, and if guards. Exhaustiveness is checked by the Rust compiler.