Text Templates
Text Templates
HatTrick Text Templates is a text templating engine for .NET. Templates use a single-bracket {tag} syntax. The library targets .NET 9 or later and has one NuGet dependency: HatTrick.Reflection.
Why Text Templates
- Small fixed grammar — ten tag types: simple bind, conditional, iteration, scope shift, partial, variable (declare, reassign, read), comment, and debug.
- Lambda calls — any tag can invoke a registered
Func<…, string>supplied by the host application, keeping formatting, encoding, and other logic out of the template syntax. - Flexible binding — bind against properties, fields, or string-keyed
IDictionaryentries. Dotted expressions ({Address.City}) traverse nested graphs. - Whitespace control — per-tag
-trim markers, per-tag+retain markers, and a globalTrimWhitespacetoggle. - No upfront compile — templates are scanned forward during
Merge. No AST, no IL, no compiled state. A template changed at runtime takes effect on the next call. - Constant merge state — the engine retains no per-template state between calls. Memory footprint and per-merge cost are stable.
- One error path — every failure surfaces at merge time as a
MergeExceptionwith line, column, char index, and last-parsed tag. - Bounded stack depth — nested partials and block tags are capped, preventing stack-overflow from untrusted template content.
When to use
Good fit:
- Code, SQL, or markup generation from structured .NET data
- Email, report, and document assembly
- CLI tools and code generators that emit text artifacts
- Templates that are data — loaded from a database, user-authored, or selected at runtime
- Serverless functions (AWS Lambda, Azure Functions) — no precompiled state means no cold-start overhead
Not a good fit:
- Full in-template programmability (statements, method bodies, type definitions)
- HTML/XML-aware rendering with built-in encoding context
- Pipelines that depend on a compiled template artifact (IL inspection, delegate emission)
- Built-in localization, pluralization, or formatting catalogs
Where to find Text Templates
| Resource | Location |
|---|---|
| GitHub | github.com/HatTrickLabs/txt |
| NuGet | HatTrick.Text.Templating |
Documentation
Install the package and render your first template.
Tag delimiters, local scope, bracket escaping, and falsy semantics.
Single-tag replacement and compound bind expressions for nested data.
{#if} blocks with negation and falsy short-circuit.
{#each} over any IEnumerable with per-item local scope.
The ..\ operator for reaching outer scopes from nested blocks.
{#with} scope shifts for reusable sub-templates.
Declare, reassign, and read template-local variables.
Inject sub-template content, including templates bound from data.
{!} comment tags with multiline and bracket-escape rules.
Trim markers, retain markers, and global TrimWhitespace.
Register functions and call them from any tag.
{@} debug tag and System.Diagnostics.Trace integration.
MergeException, context frames, and pinpointing failures.
Bounded nesting as a defense against stack-overflow attacks.