Build a prompt template engine that supports variables, conditionals, loops, and includes. Like Handlebars but for LLM prompts.
## Task Build a prompt template engine for LLM applications. ## Requirements - Language: TypeScript - Zero dependencies (or minimal: just a parser) - Type-safe variable injection ## Template Syntax ``` Simple variables: {{name}}, {{company}} Conditionals: {{#if premium}}...{{else}}...{{/if}} Loops: {{#each items}}{{this.name}}: {{this.value}}{{/each}} Includes: {{> partials/header}} Defaults: {{name | default: "User"}} Transforms: {{name | uppercase}}, {{text | truncate: 100}} Comments: {{! This is ignored }} ``` ## API ```typescript const engine = new PromptEngine(); // Register template engine.register("review-prompt", ` You are reviewing {{product}} for {{#if expert}}an expert{{else}}a general{{/if}} audience. {{#each criteria}} - Evaluate: {{this}} {{/each}} `); // Render const prompt = engine.render("review-prompt", { product: "iPhone 16", expert: true, criteria: ["design", "performance", "value"], }); // Validate — check all required variables are provided engine.validate("review-prompt", variables); // throws if missing ``` ## Implementation Notes 1. Parse template into AST (tokens: text, variable, block-open, block-close) 2. Compile AST into a render function for performance 3. Type-safe: infer variable types from template 4. Escape HTML/XML in variables by default (opt-out with {{{raw}}}) 5. Max nesting depth of 5 to prevent infinite loops 6. Include a CLI tool: `prompt-render template.txt data.json`
No gallery images yet.