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

Implementation:AGenUI AGenUI TemplateRegistry

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

Overview

Template registry that loads component templates from JSON files, expands them by replacing `@placeholder()` tokens with caller-supplied values, and prefixes all component IDs to avoid collisions, provided by the AGenUI C++ core.

Description

The `TemplateRegistry` class manages the template expansion lifecycle for reusable UI component patterns. It maintains a set of registered template names (loaded from `templates.json` in the template directory) and provides expansion of template components into concrete component JSON strings.

The expansion pipeline works as follows:

  1. `isTemplate(name)` — lazy-loads the template registry from `templates.json` (via `IEngineContext::getPathConfig()`) and checks if a component name is a registered template.
  2. `expandTemplate(component)` — loads the template JSON file (`<name>.json`), extracts placeholder values from the input component (all properties except `id` and `component`), replaces `@placeholder(token)` patterns in the template, and prefixes all component IDs.
  3. `replacePlaceholders()` — recursively walks the template JSON, replacing `@placeholder(name)` in string values with the corresponding input values. Supports path suffixes: `@placeholder(base)/path` concatenates the suffix. Non-string values are replaced wholesale.
  4. `prefixComponentIds()` — prepends the input component's ID to all template component IDs and child references to avoid duplicate IDs when the same template is used multiple times. Special handling for root nodes and `Tabs` component `tabs[].child` references.

The result is an `ExpandedTemplate` containing a list of component JSON strings and display rules per component ID.

Usage

  • Custom components: when a component's type matches a registered template name, `TemplateRegistry` expands it into concrete components during surface creation.
  • Reusable patterns: templates allow defining UI patterns (cards, forms, dialogs) as JSON files with placeholders for data injection.
  • ID collision avoidance: all expanded IDs are prefixed with the caller's component ID.

Code Reference

Source Location

Signature

// core/src/surface/agenui_template_registry.h
namespace agenui {

struct ExpandedTemplate {
    std::vector<std::string> components;              // Expanded component JSON strings
    std::map<std::string, DisplayRule> displayRules;  // Display rules per componentId
};

class TemplateRegistry {
public:
    TemplateRegistry();
    ~TemplateRegistry();

    // Lifecycle management
    bool initialize();
    bool start();
    void stop();
    void shutdown();

    /// Determine if a component is a registered template
    bool isTemplate(const std::string& componentName);

    /// Expand template into a list of component JSON strings
    /// @param component Input with "component" = template name, plus placeholder values
    /// @param isNonRootNode If true, root component ID is replaced (not prefixed)
    /// @return Expanded template with component strings and display rules
    ExpandedTemplate expandTemplate(const nlohmann::json& component,
                                     bool isNonRootNode = false);

private:
    void loadTemplateRegistry();
    nlohmann::json loadTemplateFile(const std::string& templateName);
    std::map<std::string, nlohmann::json> extractPlaceholderValues(
        const nlohmann::json& component);
    void replacePlaceholders(nlohmann::json& node,
                             const std::map<std::string, nlohmann::json>& values);
    void prefixComponentIds(nlohmann::json& components,
                            const std::string& prefix,
                            bool isNonRootNode = false);

    std::set<std::string> _templateRegistry;
    bool _registryLoaded = false;
};

} // namespace agenui

Import

#include "surface/agenui_template_registry.h"

I/O Contract

Inputs

Name Type Required Description
componentName `const std::string&` Yes (isTemplate) Component type name to check against registry
component `const nlohmann::json&` Yes (expandTemplate) JSON with "component" (template name), "id", and placeholder values
isNonRootNode `bool` No If true, root component ID is replaced instead of prefixed (default false)

Outputs

Name Type Description
isTemplate returns `bool` True if component name is a registered template
expandTemplate returns `ExpandedTemplate` Vector of component JSON strings + display rules map
ExpandedTemplate.components `vector<string>` List of expanded component JSON strings
ExpandedTemplate.displayRules `map<string, DisplayRule>` Display rules keyed by component ID (root gets AnyDataReady)

Usage Examples

Checking and Expanding a Template

#include "surface/agenui_template_registry.h"
#include "agenui_engine_context.h"

auto* ctx = agenui::getEngineContext();
auto* templateRegistry = ctx->getTemplateRegistry();

// Input component referencing a template
nlohmann::json input = R"({
    "component": "ProductCard",
    "id": "card1",
    "title": "Widget Pro",
    "price": "$29.99",
    "imageUrl": "/img/widget.png"
})"_json;

if (templateRegistry->isTemplate("ProductCard")) {
    auto expanded = templateRegistry->expandTemplate(input);

    // expanded.components contains the expanded component JSON strings
    // All IDs are prefixed with "card1" (e.g. "card1_title", "card1_price")
    for (const auto& compJson : expanded.components) {
        printf("Component: %s\n", compJson.c_str());
    }
}

Template File Structure

// templates.json (template directory)
{
    "templates": ["ProductCard", "UserAvatar", "CommentBox"]
}

// ProductCard.json (template file)
{
    "components": [
        {
            "component": "Column",
            "id": "root",
            "children": [
                {"component": "Text", "id": "title", "text": "@placeholder(title)"},
                {"component": "Text", "id": "price", "text": "@placeholder(price)"},
                {"component": "Image", "id": "image", "src": "@placeholder(imageUrl)/thumb"}
            ]
        }
    ]
}

After expansion with the input above, IDs become `card1`, `card1_title`, `card1_price`, `card1_image`, and `@placeholder(imageUrl)/thumb` becomes `/img/widget.png/thumb`.

Related Pages

Implements Principle

Requires Environment

Related Implementations

Page Connections

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