Lesson 4 — Yielding: the single yield point
So far all our components render entirely from their props. But what about components that need to contain arbitrary content — a panel with a list, a card with a table inside it? We cannot pass rich HTML as a prop safely. We need yielding.
The simplest case
|
|
Usage:
|
|
Output:
|
|
The block passed to the component is yielded inside the section. Whatever Phlex output the block produces appears at that point in the template.
yield(self) — what the block argument actually is
When you write:
|
|
You might assume panel is some kind of proxy object. It is not - panel is the Panel instance itself.
Phlex automatically upgrades yield to yield(self) inside view_template. This means the block receives the component as its argument, allowing callers to invoke methods on the component from within the block. We will use this extensively in Lesson 5 for named slots.
Adding Panel to the demo
|
|
The third panel demonstrates composition — Badge components used freely inside the yield block of a Panel.
Exercise
Add a fourth panel to show_panels that renders a nested Button and a short paragraph of text inside it. This reinforces that any component can be composed inside a yield block.
Solution
|
|