Typed prompt templates for LLMs.
The Template
--- consts: - MODEL = str := "gemini-3.5-flash" - MAX_FINDINGS = int := 20 types: - Severity = enum(Critical, High, Medium, Low) - Verdict = enum(Approved, NeedsChanges(reason = str), Rejected(reason = str)) params: - reviewer = str - file_path = str - severity = Severity - findings = list(line = int, message = str, severity = Severity) - verdict = Verdict --- # Code Review: {{ file_path }} Reviewer: {{ reviewer }} · Model: {{ MODEL }} Severity: {{ kind(severity) }} · Findings: {{ len(findings) }}/{{ MAX_FINDINGS }} > {% for finding in findings %} - **L{{ finding.line }}** ({{ kind(finding.severity) }}): {{ finding.message }} > {% /for %} > {% match verdict %} > {% case Approved %} ✅ **Approved** — ship it. > {% case NeedsChanges %} 🔄 **Needs changes:** {{ verdict.reason }} > {% case Rejected %} ❌ **Rejected:** {{ verdict.reason }} > {% /match %}
Types and fields are validated at build time — rendering happens at runtime:
use md_tmpl::include_template; include_template!("prompts/code_review.tmpl.md"); let output = code_review::Params::builder() .reviewer("Alice") .file_path("src/main.rs") .severity(code_review::Severity::High) .findings([ code_review::ParamsFindingsItem::builder() .line(42) .message("unused import") .severity(code_review::Severity::Low) .build(), ]) .verdict(code_review::Verdict::NeedsChanges { reason: "remove dead imports".into(), }) .build().render().unwrap();
Wrong types, missing fields, non-exhaustive matches — all caught at build time. Templates can also be loaded and validated at runtime for dynamic or hot-reload use cases.
Features
| Feature | Description |
|---|---|
| Typed parameters | str (untrusted by default), trusted str, int, int(lo..=hi), int enum(...), float, float(lo..=hi), bool, list(…), struct(…), enum(…), option(…), tmpl(…) |
| Compile-time verification | Checked const/condition arithmetic, {% assert COND : "msg" %} with has() narrowing, frontmatter test-defaults: / test-matrix:, and finite-domain unreachable-branch analysis |
| Prompt-injection hardening | str values are sanitized wherever they are emitted (control tokens, invisible characters, enclosing-tag breakouts); trust follows the value through includes; trusted str and unsafe_trust(…) opt out explicitly; sanitize / quarantine filters |
| Type aliases | types: defines reusable named types |
| Cross-template imports | imports: pulls types via dotted paths (stem.TypeName) |
| Typed lists | list(title = str, score = int) — iterate with {% for %}, fields validated |
| Enum dispatch | match/case with exhaustiveness checking and field narrowing |
| Includes as links | {% include [name](path.tmpl.md) with … %} — clickable, type-checked |
| Inline templates | {% tmpl name %} — reusable fragments without separate files |
| Constants | consts: for file-scoped immutable values |
| Environment variables | env: for compile-time injection from the build environment |
| String interpolation | {{ expr }} inside all quoted strings — conditions, includes, panic messages |
| Built-in functions | idx(b), len(x), kind(x), kinds(t), has(x), unsafe_trust(x) + filters (upper, lower, trim, fixed, join, limit, truncate, add, sub, xml, escape_json, json, sanitize, sanitize_tokens, quarantine, fence, unsafe_trust) |
| Readable as markdown | > {% %} blockquote prefix keeps control flow visually separated from prose |
| Markdown-safe syntax | Valid YAML frontmatter and () compound type syntax |
Note: The
>prefix is required only on{% %}tag lines — it is stripped before compilation. Content lines between tags are normal text and should not use>. A content line starting with>is kept verbatim as a literal markdown blockquote in the output.
Quick Start
use md_tmpl::include_template; // Parsed + validated at build time — generates typed structs from frontmatter include_template!("prompts/task_report.tmpl.md"); let output = task_report::Params::builder() .title("Sprint 42") .priority(task_report::Priority::Critical) .tasks([ task_report::ParamsTasksItem::builder() .name("Fix auth bypass") .urgency(task_report::Priority::Critical) .build(), task_report::ParamsTasksItem::builder() .name("Update dependencies") .urgency(task_report::Priority::Low) .build(), ]) .build().render().unwrap();
Performance
The Rust core renders into a single pre-sized output buffer (reuse it across renders with the render_*_into variants to avoid per-render output allocation), backed by a native FFI engine in Python, Go, and WebAssembly.
Rust (render-only, pre-parsed)
| Scenario | md-tmpl (macro) | md-tmpl | Tera | MiniJinja |
Handlebars |
|---|---|---|---|---|---|
| simple | 21 ns 🏆 | 178 ns | 244 ns | 621 ns | 717 ns |
| loop | 35 ns 🏆 | 476 ns | 640 ns | 1.80 µs | 3.12 µs |
| conditional | 21 ns 🏆 | 223 ns | 377 ns | 673 ns | 1.23 µs |
| hero | 178 ns 🏆 | 1.83 µs | 2.69 µs | 7.82 µs | 23.63 µs |
| mega | 769 ns 🏆 | 7.45 µs | 10.96 µs | 29.91 µs | 87.36 µs |
See the language-specific READMEs or the benchmarks suite for full details and methodology.
Language Bindings
Native packages across Rust, Python, Go, TypeScript, and WebAssembly:
- Rust — Reusable-buffer rendering (
render_intowrites into your ownString), build-time validation via proc macros (include_template!), built-in zero-dependency typestate builders &serdeintegration,ctx!macro, caching, and hot-reload (backed by the shared#![no_std]tmpl-corefoundation). - Python — 3–7× faster than Jinja2 for rendering (30–60× faster on cold parse). Auto-generated dataclasses, mypy/pyright static typing stubs, native Python 3.10+ structural pattern matching (
match/case), and direct import hooks (from prompts.review import CodeReview). - Go — Up to 5–6× faster than standard
text/templateon medium and large templates. Native Rust FFI engine, idiomatic struct tag mapping (json:"field"), map rendering, and static enum typing viaTaggedVariant. - TypeScript — 2–8× faster than Handlebars and 1.3–6× faster than Mustache on pre-parsed rendering (with full runtime schema validation). Build-time TypeScript type generation (
generateTypesFromFile), genericTypedTemplate<P>contracts, type-safe enum constructors (defineVariants), and exhaustive pattern matching (match). - WASM — Exact Rust-engine feature parity in Node.js and browsers (~200 KB
.wasm). Supports zero-copy binary throughput (renderFlexbuffers) and implements the exact sameITemplateinterface as pure TypeScript.
Full Reference
See SPEC.md for the complete syntax — all control-flow tags, filters, built-in functions, whitespace control, and error diagnostics.
Packages
- Rust: https://crates.io/crates/md-tmpl (plus https://crates.io/crates/tmpl-core, https://crates.io/crates/md-tmpl-core, and https://crates.io/crates/md-tmpl-macros)
- Python: https://pypi.org/project/md-tmpl/
- Go: https://github.com/domenukk/md-tmpl
- TypeScript: https://www.npmjs.com/package/md-tmpl
License
Apache-2.0 OR MIT