If, Elif, and Else let you conditionally render different components based on expression results. They mirror Python’s own if/elif/else syntax and read the same way — no wrappers, no extra indentation.
Basic Usage
The simplest case: show something only when a condition is true. Toggle the switch to see the alert appear and disappear. A loneIf renders its children when the condition is truthy, and renders nothing when it’s falsy.
If / Else
Add anElse branch to render fallback content when the condition is false. Toggle the switch to see the two branches swap.
If / Elif / Else
Chain multiple conditions withElif. The renderer evaluates them in order and renders the first match. If nothing matches, the Else branch renders. Use the select below to change the status and watch the right badge appear.
Select “Error” or “Something else” to see different branches render. The Else catches anything that doesn’t match the explicit conditions.
Conditions Are Expressions
The first argument toIf and Elif is always a raw expression, not a {{ }} template. Templates embed expressions inside larger strings (Text("Hello, {{ name }}!")), but a condition is the expression — there’s nothing to embed it in.
Chain Rules
ConsecutiveIf/Elif/Else siblings in the same parent form a single conditional chain:
- A chain starts with
Ifand extends through consecutiveElifandElsesiblings ElifandElseare optional — a loneIfis validElsemust be last in a chain- Any non-conditional sibling breaks the chain — including another
If, which starts a new independent chain
Condition nodes with a Text between the first and second.
Wire Format
In the Python DSL, you write naturalIf/Elif/Else siblings. During serialization, these are grouped into a single Condition node on the wire. The If and Elif components never appear in the JSON directly — they’re purely authoring constructs.
Each Elif becomes a case entry with a when expression. The Else branch becomes the else field. See the Condition protocol reference for the full schema.
API Reference
If
Expression to evaluate. Renders children when truthy. Can be passed as a positional argument.
Elif
Expression to evaluate. Must follow an
If or another Elif. Can be passed as a positional argument.Else
Renders children when no preceding
If or Elif matched. Takes no condition argument.