Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AGenUI AGenUI Template Expansion

From Leeroopedia
Knowledge Sources
Domains Component_System, Parsing
Last Updated 2026-10-24 17:21 GMT

Overview

A component expansion technique that replaces `@placeholder()` tokens in a JSON template file with caller-supplied values and rewrites all component IDs to avoid collisions, enabling reusable UI patterns in a declarative component system.

Description

In a declarative UI framework, templates are reusable component patterns defined as JSON files. A template contains placeholder tokens (`@placeholder(name)`) that are replaced with concrete values supplied by the caller at expansion time. This allows a single template (e.g. "ProductCard") to be instantiated many times with different data.

The expansion process has three stages:

  1. Placeholder extraction: all properties from the input component (except `id` and `component`) are collected as a key-value map.
  2. Placeholder replacement: the template JSON is walked recursively. In string values, `@placeholder(name)` patterns are replaced with the corresponding value. Path suffixes (`@placeholder(base)/path`) are concatenated. Non-string values replace the entire node.
  3. ID prefixing: all component IDs and child references in the expanded template are prefixed with the caller's component ID, preventing collisions when the same template is used multiple times in the same surface.

Usage

Use this principle when:

  • A UI framework needs reusable component patterns that can be parameterized at use sites
  • The same template may be instantiated multiple times in one surface without ID conflicts
  • Template authors need path-suffix concatenation for URL/path assembly from placeholder values

Theoretical Basis

# Abstract algorithm (NOT real implementation)
def expand_template(input_component):
    template_name = input_component["component"]
    template_json = load_template_file(template_name)

    # 1. Extract placeholder values (everything except id and component)
    values = {k: v for k, v in input_component.items()
              if k not in ("id", "component")}

    # 2. Replace placeholders recursively
    replace_placeholders(template_json, values)

    # 3. Prefix all IDs with caller's ID
    caller_id = input_component.get("id", "")
    prefix_component_ids(template_json["components"], caller_id)

    return [comp.dump() for comp in template_json["components"]]

def replace_placeholders(node, values):
    if node is string:
        # Replace @placeholder(name) and @placeholder(name)/suffix
        for match in find_placeholders(node):
            value = values[match.name]
            if match.has_suffix and value is string:
                node = node.replace(match, value + match.suffix)
            elif value is string:
                node = node.replace(match, value)
            else:
                node = value  # non-string replaces wholesale
    elif node is object:
        for child in node.values():
            replace_placeholders(child, values)
    elif node is array:
        for element in node:
            replace_placeholders(element, values)

Practical Guide

  • Template files are JSON files in the template directory (configured via `PathConfig`).
  • The registry of available template names is loaded from `templates.json`.
  • Placeholder pattern: `@placeholder(propertyName)` in any string value.
  • Path suffix: `@placeholder(baseUrl)/thumbnail` concatenates the suffix to the string value.
  • Non-string placeholder values (numbers, objects, arrays) replace the entire string node.
  • ID prefixing handles `id`, `child`, `children` (array of strings or object with `componentId`), and `Tabs.tabs[].child`.

Related Pages

Implemented By

Related Principles

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment