Templates
Templates are reusable component blueprints defined in YAML. They are the primary way to extend yamlCase with new component types. No TypeScript or Python required. Once a template is created, it becomes available as a type in all your views.
How Templates Work
How Templates Work
The template system has three simple steps:
1. Define
Create a YAML file in your templates/ directory with a component definition including name, props, and render tree.
2. Compile
Approved compilation resolves your templates and generates typed React components.
3. Use
Reference your template by name as a type in any view. Pass props as YAML keys.
Template Structure
Template File Structure
Every template YAML file has a component key with four main sections. Here is a complete example.
# templates/info_box.yaml
component:
name: InfoBox
tag: div
className: info-box
props:
title:
type: string
required: true
message:
type: string
variant:
type: string
default: info
render:
- tag: strong
content: "Complete Template Example"
- tag: p
content: "{{message}}"
- slot: children
Let us break down each section:
Name, Tag, and ClassName
The name field sets the React component name (use PascalCase). The tag sets the root HTML element, and className adds CSS classes to it.
component:
name: StatusBadge
tag: span
className: badge badge--{{variant}}
Props
Defining Props
Props define the data your component accepts. Each prop has a type, and optionally a default value and required flag. The compiler generates TypeScript interfaces from these definitions.
props:
title:
type: string
required: true
variant:
type: string
default: info
dismissible:
type: boolean
default: false
maxItems:
type: number
default: 10
subtitle:
type: string
This generates the following TypeScript interface:
interface MyComponentProps {
title: string;
variant?: string;
dismissible?: boolean;
maxItems?: number;
subtitle?: string;
children?: React.ReactNode;
}
| Prop Type | TypeScript Type | Example Default |
|---|---|---|
| string | string | info |
| number | number | 10 |
| boolean | boolean | false |
| array | any[] | [] |
| object | Record<string, any> | null |
Render Tree
The Render Tree
The render array defines the HTML structure of your component. Each item is a node with a tag, optional className, content, children, and when for conditional rendering.
render:
- tag: h3
className: card-title
content: "Render Tree Example"
- tag: p
className: card-subtitle
content: "{{subtitle}}"
when: subtitle
- tag: div
className: card-body
children:
- slot: children
- tag: div
className: card-footer
when: footer
children:
- tag: span
content: "{{footer}}"
Prop Interpolation
Use double braces around propName to insert prop values into content, className, or any attribute. The compiler replaces these with JSX expressions.
- tag: div
className: alert alert--{{variant}}
children:
- tag: strong
content: "YAML Template Syntax"
- tag: p
content: "{{message}}"
This compiles to the following TSX:
- tag: div
className: alert alert--{{variant}}
children:
- tag: strong
content: "YAML Template Syntax"
- tag: p
content: "{{message}}"
This compiles to the following TSX:
<div className={`alert alert--${props.variant}`}>
<strong>{props.title}</strong>
<p>{props.message}</p>
</div>|
<div className={`alert alert--${props.variant}`}>
<strong>{props.title}</strong>
<p>{props.message}</p>
</div>
Conditional Rendering (when)
Add when: propName to any render node to conditionally show it. The node only renders when the prop is truthy.
Conditional Rendering
The subtitle paragraph only appears when subtitle is provided.
render:
- tag: h3
content: "{{title}}"
- tag: p
content: "{{subtitle}}"
when: subtitle
<h3>{props.title}</h3>
{props.subtitle && <p>{props.subtitle}</p>}
Slots
Slots for Children
Slots define where child content goes inside your component. The most common slot is children, which lets users nest content inside your component.
The slot children directive becomes props.children in TSX:
component:
name: Panel
tag: div
className: panel
props:
title: { type: string, required: true }
render:
- tag: div
className: panel-header
children:
- tag: h3
content: "YAML Template with Slot"
- tag: div
className: panel-body
children:
- slot: children
Generated TSX output:
const Panel = (props) => {
return (
<div className="panel">
<div className="panel-header">
<h3>{props.title}</h3>
</div>
<div className="panel-body">
{props.children}
</div>
</div>
);
};| - type: text
component:
name: Panel
tag: div
className: panel
props:
title: { type: string, required: true }
render:
- tag: div
className: panel-header
children:
- tag: h3
content: "YAML Template with Slot"
- tag: div
className: panel-body
children:
- slot: children
Generated TSX output:
const Panel = (props) => {
return (
<div className="panel">
<div className="panel-header">
<h3>{props.title}</h3>
</div>
<div className="panel-body">
{props.children}
</div>
</div>
);
};|
const Panel = (props) => {
return (
<div className="panel">
<div className="panel-header">
<h3>{props.title}</h3>
</div>
<div className="panel-body">
{props.children}
</div>
</div>
);
};
When using a component with slots, nest children directly:
- type: panel
title: User Settings
children:
- type: text
content: Manage your preferences below.
- type: input
label: Display Name
placeholder: Enter your name
Template CSS
Styling Templates
Templates use className for styling. The convention is to prefix classes with your component name to prevent collisions. CSS can be defined in view styles, global CSS, or a dedicated stylesheet.
component:
name: Card
tag: div
className: hhd-card
render:
- tag: div
className: card-header
- tag: div
className: card-body
- tag: div
className: card-footer
when: footer
.hhd-card {
border: 1px solid #e0e0e0;
border-radius: 8px;
overflow: hidden;
}
.card-header { padding: 1rem; border-bottom: 1px solid #e0e0e0; }
.card-body { padding: 1rem; }
.card-footer { padding: 0.75rem 1rem; background: #f5f5f5; }
Template Discovery
Where to Put Templates
yamlCase discovers templates from two locations:
Built-in Templates
Ship with yamlCase in the templates/ directory. These provide 22 template files with 85 template types.
Project Templates
Your custom templates. Add directories via template_dirs in yamlcase.config.yaml. Templates here override built-in ones.
# yamlcase.config.yaml
project:
template_dirs:
- ./my-templates
- ../shared-templates
Built-in Templates
Built-in Template Catalog
yamlCase includes 22 template files that provide 85 template types. Here are the main categories:
Layout Components
| Type | Description | Key Props |
|---|---|---|
| card | Content card with header, body, footer | title, subtitle, footer, elevated |
| grid | CSS grid layout container | columns |
| flex | Flexbox layout container | direction, gap, align, justify |
| hero | Hero/banner section | title, subtitle, background, align |
| sidebar | Sidebar navigation panel | title, width |
| navbar | Top navigation bar | title, brand |
| tabs | Tabbed content panels | tabs (array) |
| modal | Modal dialog overlay | title, open, onClose |
| accordion | Collapsible content section | title, defaultOpen |
Content Components
| Type | Description | Key Props |
|---|---|---|
| heading | H1-H6 heading element | text, level (1-6) |
| text | Paragraph text with variants | content, variant |
| image | Image with alt text | src, alt, width, height |
| divider | Horizontal rule with optional label | label |
| code_block | Formatted code display | code, language, title |
| code_example | Side-by-side YAML and TSX comparison | title, description, yaml_code, tsx_code |
| alert | Notification banner | variant, title, message, dismissible |
| stat_card | Metric display card | label, value, icon, trend |
| avatar | User avatar circle | src, name, size |
| progress_bar | Progress indicator | value, max, label, variant |
Form Components
| Type | Description | Key Props |
|---|---|---|
| input | Text input field | label, placeholder, input_type |
| select | Dropdown select | label, options, placeholder |
| form | Form wrapper with fields | fields, submitText |
| button_group | Group of action buttons | buttons (array) |
Walkthrough
Tutorial: Create a Custom Template
Let us build a StatusBanner template from scratch. A colored banner that shows a status message with an icon.
Step 1: Create the Template File
Create a file called status_banner.yaml in your templates directory:
component:
name: StatusBanner
tag: div
className: status-banner status-banner--{{variant}}
props:
variant:
type: string
default: info
icon:
type: string
message:
type: string
required: true
dismissible:
type: boolean
default: false
render:
- tag: span
className: status-banner-icon
content: "{{icon}}"
when: icon
- tag: span
className: status-banner-message
content: "{{message}}"
- tag: button
className: status-banner-close
content: "x"
when: dismissible
Step 2: Add CSS
Add styles for your template in your project CSS:
.status-banner {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.75rem 1rem;
border-radius: 6px;
}
.status-banner--info { background: #e3f2fd; color: #1565c0; }
.status-banner--success { background: #e8f5e9; color: #2e7d32; }
.status-banner--warning { background: #fff3e0; color: #e65100; }
.status-banner--error { background: #ffebee; color: #c62828; }
Step 3: Use in Your Views
Now use your new component type in any view:
- type: status_banner
variant: success
icon: "check"
message: All systems operational
dismissible: true
- type: status_banner
variant: warning
message: Scheduled maintenance at midnight
Step 4: Build
Submit your template with the views that use it through approved yamlCase access:
# Compile through approved yamlCase access, then test the React output.
Escape Syntax
Escape Syntax
To show literal double-brace syntax in rendered output, prefix with a backslash. This prevents template interpolation.
# Interpolated (replaced with prop value):
content: "Hello, {{name}}!"
# Literal text:
content: "Hello, \{{name}}!"
Best Practices
Template Best Practices
Use Semantic Tags
Choose appropriate HTML tags. Use section for sections, nav for navigation, details for collapsible content.
Prefix CSS Classes
Use a prefix like hhd- or your project name to prevent class collisions between templates.
Set Sensible Defaults
Always provide default values for optional props. This makes templates work with minimal configuration.
Add Comments
Include a comment header with usage examples. This helps other developers understand how to use the template.
Keep Templates Focused
One template, one purpose. Compose complex layouts from multiple simple templates.
Test with Views
Create a test view that exercises props and variations. Validate it through approved compilation and test the resulting React output.
Next Steps
Next Steps
YAML Spec
Learn the complete YAML format for views, including state, events, and conditionals.
Advanced
Global state, theming, form validation, and auto-generated tests.
Playground
Explore YAML examples and the managed compilation workflow.
Builder
Visual drag-and-drop interface for building YAML views.