Writing Templates
The template contract: defineTemplate with a field schema, declared fonts, and a render function.
A template is a plain React component rendered to an image by Takumi no headless browser, no custom DSL. If you can write a React component, you can write a template.
The template contract
A template file default-exports defineTemplate({ schema, fonts, render }):
schemadescribes the content: onefield.*builder per prop. It drives CLI validation, the studio's prop panel, and which props a locale file may translate.fontsnames the faces the template uses, by role. The pipeline loads every one of them.render(props, ctx)returns the image as JSX.propsare validated and filled with defaults;ctxsays what the render is (locale, direction, canvas size) and adapts styles to it.
// templates/stat-card.tsx
import { defineTemplate, field } from "@stdout-design/cli/template";
export default defineTemplate({
fonts: {
display: { faces: [{ family: "Inter", weights: [500, 800] }] },
},
schema: {
title: field.text("Weekly Active Users"),
stat: field.text("124.5K", { translatable: false }),
label: field.text("vs last 7 days"),
accentColor: field.color("#8b5cf6", { description: "Primary brand color" }),
theme: field.select(["light", "dark"], "dark", {
description: "Color theme mode",
}),
},
render: (props, ctx) => {
const isDark = props.theme === "dark";
return (
<div
tw="flex flex-col w-full h-full p-10 justify-between"
style={{
backgroundColor: isDark ? "#0f172a" : "#ffffff",
color: isDark ? "#f8fafc" : "#0f172a",
fontFamily: ctx.fonts.display,
}}
>
<p tw="text-2xl font-medium m-0">{props.title}</p>
<p
tw="m-0"
style={ctx.type({
leading: 1,
size: 128,
tracking: -0.04,
weight: 800,
})}
>
{props.stat}
</p>
<p tw="text-2xl font-medium m-0" style={{ color: props.accentColor }}>
{props.label}
</p>
</div>
);
},
});The schema never declares locale or ctx: the pipeline owns both, and a schema that names one fails to type-check.
Field builders
Each builder takes the default first and its limits as options, and records what the field is, so the studio picks the right editor without guessing from the prop's name.
| Builder | Studio control | Translatable |
|---|---|---|
field.text(default, { min, max, multiline, translatable }) | Text input, or a textarea with multiline | yes, unless translatable: false |
field.color(default) | Color picker (hex only) | no |
field.number(default, { min, max, integer }) | Number input, or a slider with min and max | no |
field.boolean(default) | Toggle switch | no |
field.select(options, default) | Dropdown | no |
field.list(item, { default, length, min, max }) | Tag editor; one group of fields per item for a list of objects | per item field |
field.object({ ... }) | Nested fields, flattened into dotted.path | per field |
Item counts belong in the schema: field.list(field.text("Item"), { default: [...], length: 6 }) refuses a seventh item at validation time instead of the layout breaking. Every builder takes a description, shown as a hint under the field in the studio and in CLI validation errors. The studio labels a field with its key in sentence case (accentColor becomes "Accent color"); pass label for a key that does not read well that way:
cta: field.text("Shop now", { label: "Call to action" }),The studio inspector
By default the studio groups a template's fields by kind (text, colors, settings, lists). Add an inspector list to name the groups and set the order of the fields in each:
export default defineTemplate({
schema,
inspector: [
["Content", ["headline", "description", "brand"]],
["Colors", ["accentColor", "background"]],
],
render,
});A field the list leaves out goes to a trailing "Other" group. Naming a key that is not in the schema, or naming a key twice, is a type error.
The render context
ctx. | What it is |
|---|---|
locale | { id, script, dir, lang } for this render |
dir | "ltr" or "rtl". The root element already carries it. |
canvas | { width, height, aspect }, with aspect one of square, landscape, portrait |
fonts | The CSS font-family value for each declared role |
flip(value) | Negates a horizontal value (a rotation, an SVG offset) for right-to-left renders |
type(spec) | A text style written for Latin, adapted to the script: see Internationalization |
Authoring kit
@stdout-design/cli/template also exports the small pieces poster templates keep rewriting: keyed(items) (unique React keys, even for repeated labels), words(text), starburst(spikes, outer, inner) (polygon points), halftone({ color, spacing }) (a dot-screen background), <Arrow ctx={ctx} /> (points forward in either direction), and <Sparkle />.
Templates written before defineTemplate
A file that default-exports a component and exports a propsSchema (a z.object) still renders, and still receives locale as a prop, until 1.0. The pipeline doesn't set dir on its root, since such templates set it themselves.
Registering a template
A template is only renderable once it's registered in studio.config.ts:
// studio.config.ts
import type { StudioConfig } from "@stdout-design/core";
const config: StudioConfig = {
templates: {
"stat-card": {
componentPath: "./templates/stat-card",
description: "A milestone or stat card for social media.",
},
},
presets: [
{
id: "instagram-square",
width: 1080,
height: 1080,
platform: "instagram",
},
],
};
export default config;- The key (
"stat-card") is the template ID you pass torender. componentPathpoints at the.tsxfile without the extension.descriptionis optional and shows up in the studio's template dropdown.
If a template fails to load, other templates keep working errors surface per-template in the studio and on the CLI.
Validation
- Defaults. Every builder takes a default, so templates render with zero required arguments. The studio seeds the defaults; the CLI merges them before rendering.
- Errors. Invalid props, including a locale file's text that breaks a field's
max, fail the render with a structuredPropValidationError, which agents can use to self-correct (see AI Agents).
Styling
Templates use Tailwind-style utilities via the tw attribute, plus regular inline style for anything dynamic:
<div
tw="flex items-center justify-between w-full h-full p-10"
style={{ backgroundColor: "#0a0a0b", color: "#ffffff" }}
>The scaffold's types.d.ts augments React's DOMAttributes with the tw?: string prop, so tw= type-checks out of the box.
Supported styling features include flexbox, grid, absolute positioning, z-index, border-radius, background images (including gradients), SVG, and dynamic values computed inside the component (see the sparkline example below).
Working with data
render is a plain function returning JSX, so you can map over arrays, compute styles, and render conditionally:
{
keyed(props.sparkline).map(({ key, value }) => (
<div
key={key}
tw="w-12 rounded-t-md"
style={{
backgroundColor: props.accentColor,
height: `${(value / Math.max(...props.sparkline)) * 100}%`,
}}
/>
));
}Images
Reference remote images with a regular <img> element (also works for backgroundImage/maskImage URLs in style). The src is usually a field.text prop:
<img src={props.avatarUrl} alt="" tw="w-12 h-12 rounded-full" />Images are fetched at render time, so they must be reachable from wherever you render (locally, or in CI).
Remote images don't load until you allow them. By default, any <img src>, backgroundImage, or maskImage pointing at a URL is blocked add an allowUrl check to studio.config.ts for the domains you use:
// studio.config.ts
const config: StudioConfig = {
images: {
allowUrl: (url) => url.startsWith("https://cdn.example.com/"),
},
// ...
};See Configuration → images for every option (maxBytes, timeout, custom fetch, emoji).
Locale & RTL
The root element gets dir and lang for the render's locale, so flex rows, text alignment, and logical CSS properties (insetInlineStart, marginInlineEnd, borderInlineStart) mirror on their own. Use ctx.flip() for rotations and SVG offsets, and ctx.type() for display text. Locale files translate text fields automatically: see Internationalization.