Skip to content

Control Flow ($if, $for)

PyWire provides special attributes for controlling the structure of your HTML.

Use $if to conditionally include an element in the DOM.

---
user = wire(None)
---
<div $if={user}>
Welcome back, {user.name}!
</div>

Use $for to render a list of items.

---
items = wire(["Apple", "Banana", "Cherry"])
---
<ul>
<li $for={item in items}>
{item}
</li>
<li $if={len(items) == 0}>
No items found.
</li>
</ul>

Unlike $if, which adds or removes elements from the DOM, $show toggles the display: none CSS property. Use this for elements that need to toggle frequently without full DOM reconstruction.

---
is_visible = wire(False)
---
<div $show={is_visible}>
I'm hidden but still in the DOM!
</div>