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 IDictionary entries. Dotted expressions ({Address.City}) traverse nested graphs.
  • Whitespace control — per-tag - trim markers, per-tag + retain markers, and a global TrimWhitespace toggle.
  • 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 MergeException with 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

ResourceLocation
GitHubgithub.com/HatTrickLabs/txt
NuGetHatTrick.Text.Templating

Documentation