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.

Why Templates?

yamlCase ships with 22 template files providing 85 template types. You can create your own templates to add custom components specific to your project.


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.

yamlComplete Template 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.

yamlRoot Element Configuration
component:
  name: StatusBadge
  tag: span
  className: badge badge--{{variant}}
Dynamic classNames

You can use {{propName}} interpolation in className to create variant-based styling. For example, badge--info with variant=success becomes badge--success.


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.

yamlProp Types
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:

typescriptGenerated TypeScript Interface
interface MyComponentProps {
  title: string;
  variant?: string;
  dismissible?: boolean;
  maxItems?: number;
  subtitle?: string;
  children?: React.ReactNode;
}
Prop TypeTypeScript TypeExample Default
stringstringinfo
numbernumber10
booleanbooleanfalse
arrayany[][]
objectRecord<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.

yamlRender Tree Example
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.

yamlYAML Template Syntax
- tag: div
  className: alert alert--{{variant}}
  children:
    - tag: strong
      content: "YAML Template Syntax"
    - tag: p
      content: "{{message}}"

This compiles to the following TSX:

yamlYAML Template Syntax
- tag: div
  className: alert alert--{{variant}}
  children:
    - tag: strong
      content: "YAML Template Syntax"
    - tag: p
      content: "{{message}}"

This compiles to the following TSX:

typescriptGenerated TSX Output
<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.

YAML Input
render:
  - tag: h3
    content: "{{title}}"
  - tag: p
    content: "{{subtitle}}"
    when: subtitle
Generated TSX
<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:

yamlYAML Template with Slot
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:

typescriptGenerated TSX
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
yamlYAML Template with Slot
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:

typescriptGenerated TSX
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:

yamlUsing a Component with Slots
- 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.

yamlCSS Class Convention
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
cssCompanion CSS
.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.

yamlTemplate Directory Configuration
# yamlcase.config.yaml
project:
  template_dirs:
    - ./my-templates
    - ../shared-templates
Override Built-in Templates

If you create a template with the same name as a built-in one, yours takes priority. This lets you customize the built-in card, alert, etc. for your design system.


Built-in Templates

Built-in Template Catalog

yamlCase includes 22 template files that provide 85 template types. Here are the main categories:

Layout Components

TypeDescriptionKey Props
cardContent card with header, body, footertitle, subtitle, footer, elevated
gridCSS grid layout containercolumns
flexFlexbox layout containerdirection, gap, align, justify
heroHero/banner sectiontitle, subtitle, background, align
sidebarSidebar navigation paneltitle, width
navbarTop navigation bartitle, brand
tabsTabbed content panelstabs (array)
modalModal dialog overlaytitle, open, onClose
accordionCollapsible content sectiontitle, defaultOpen

Content Components

TypeDescriptionKey Props
headingH1-H6 heading elementtext, level (1-6)
textParagraph text with variantscontent, variant
imageImage with alt textsrc, alt, width, height
dividerHorizontal rule with optional labellabel
code_blockFormatted code displaycode, language, title
code_exampleSide-by-side YAML and TSX comparisontitle, description, yaml_code, tsx_code
alertNotification bannervariant, title, message, dismissible
stat_cardMetric display cardlabel, value, icon, trend
avatarUser avatar circlesrc, name, size
progress_barProgress indicatorvalue, max, label, variant

Form Components

TypeDescriptionKey Props
inputText input fieldlabel, placeholder, input_type
selectDropdown selectlabel, options, placeholder
formForm wrapper with fieldsfields, submitText
button_groupGroup of action buttonsbuttons (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:

yamltemplates/status_banner.yaml
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:

cssCSS for StatusBanner
.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:

yamlUsing StatusBanner in a 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:

bashBuild
# Compile through approved yamlCase access, then test the React output.
Done!

Your custom StatusBanner component is now a first-class yamlCase type. It gets TypeScript interfaces, prop validation, and works everywhere any built-in type works.


Escape Syntax

Escape Syntax

To show literal double-brace syntax in rendered output, prefix with a backslash. This prevents template interpolation.

yamlEscaping 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.