Getting Started

Requirements

Hat Trick Text Templates targets .NET 9 or later. The package has one runtime dependency (HatTrick.Reflection) which NuGet resolves automatically.

Installation

dotnet add package HatTrick.Text.Templating

Namespace: HatTrick.Text.Templating

The NuGet package and the public API share the same name:

using HatTrick.Text.Templating;

A complete first program

The engine is constructed with a template string, then merged against a data object. The bound object can be anything — an anonymous type, a class instance, a dictionary, or a more complex graph.

using System;
using HatTrick.Text.Templating;

class Program
{
    static void Main()
    {
        var data = new
        {
            FirstName = "John",
            LastName = "Doe",
            Address = new { City = "Dallas", State = "TX" }
        };

        string template = "Hello {FirstName} {LastName}, we see you currently live in {Address.City}, {Address.State}.";

        var ngin = new TemplateEngine(template);
        string result = ngin.Merge(data);

        Console.WriteLine(result);
    }
}

Expected output:

Hello John Doe, we see you currently live in Dallas, TX.

Anatomy of a merge

  • Initialize — construct TemplateEngine with the template string; no parsing occurs at construction.
  • Merge — call Merge(object) to scan the template and bind against the supplied object, producing the output string.
  • Reuse — the same instance can merge against different data objects; state resets at the start of each call.

Next steps