A lightweight .NET template engine for LLM with messages and text templates support.
$ dotnet add package LLTSharpA flexible and expressive template engine for Large Language Model (LLM) prompts and structured message generation in C#.
LLT is designed to make prompt engineering and content generation as powerful and maintainable as regular C# code.
@if, @foreach, expressions, metadata, and inline variablesInstall the package from NuGet:
dotnet add package LLTSharp
Or via the Package Manager Console:
Install-Package LLTSharp
var parser = new LLTParser();
var templateStr = """
@template GreetingTemplate
{
Greetings, @name!
@if age > 18
{
You are an adult.
}
else
{
You are too young!
}
Have a nice day.
}
""";
var template = parser.Parse(templateStr).First();
var adult = new { name = "Andrew", age = 20 };
var young = new { name = "Alice", age = 15 };
Console.WriteLine(template.Render(adult));
Console.WriteLine(template.Render(young));
Output:
Greetings, Andrew!
You are an adult.
Have a nice day.
Greetings, Alice!
You are too young!
Have a nice day.
Multiple templates can be stored, versioned, and retrieved by language or model ID:
var lib = new TemplateLibrary();
lib.ImportFromString("""
@template greeting
{
@metadata { lang: 'en' }
Hello!
}
@template greeting
{
@metadata { lang: 'en', model: 'gpt-4' }
Hello GPT-4!
}
@template greeting
{
@metadata { lang: 'es' }
Hola!
}
""");
var template = lib.Retrieve("greeting", new LanguageMetadata("en"), new TargetModelMetadata("gpt-4"));
Console.WriteLine(template.Render()); // Hello GPT-4!
Templates are resolved by metadata specificity, similar to CSS selector priority.
LLT supports special @messages syntax for structured chat prompts:
@messages template ChatBot
{
@metadata { language: 'en', version: 1 }
@system message {
You are a helpful assistant.
Here is your instructions:
@foreach instruction in instructions {
Instruction: @instruction
}
}
@foreach name in names {
@message {
@role 'user'
Hello, I am @name!
}
}
}
Rendered result: a sequence of chat messages with proper roles (system, user, assistant).
@(a + b * c)@if cond {
...
} else if other {
...
} else {
...
}
@foreach item in items {
@item
}
@let x = 5
@x = 10
@x
@render 'other_template'
@// line comment
@*
block comment
*@
@let variables are lexically scoped@if and @foreach blocks behave predictably, matching C#‑like logical semanticsTemplateNode, TemplateExpressionNode, etc.)system, user, assistant)MIT License © 2025