Define and Use solve this: Define captures a component template, Use references it with scoped data.
Define and Use
Define
Define captures a component subtree as a named template. It uses the context manager like any container, but it does not attach itself to a parent — it lives outside the component tree.
UIResponse via the defs parameter:
Use
Use references a Define by name and provides scoped data through kwargs. Any kwargs that aren’t base component fields (css_class) become interpolation values scoped to the template.
How It Works on the Wire
Define and Use desugar completely before reaching the renderer — the renderer never sees “Define” or “Use” nodes.Define serializes to the template body itself. Its name becomes a key in the defs envelope:
Use serializes to a $ref node. Without overrides, it’s simply:
State node that scopes the interpolation values:
$ref nodes by looking up the definition and rendering it with the current interpolation context. Circular references are detected and short-circuited.
With ForEach
Define/Use pairs well withForEach when you have a reusable template that also appears outside the loop:
Define with ForEach