Meziantou.Framework.Templating 2.0.14

Prefix Reserved
dotnet add package Meziantou.Framework.Templating --version 2.0.14
                    
NuGet\Install-Package Meziantou.Framework.Templating -Version 2.0.14
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Meziantou.Framework.Templating" Version="2.0.14" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.Templating" Version="2.0.14" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Framework.Templating" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Meziantou.Framework.Templating --version 2.0.14
                    
#r "nuget: Meziantou.Framework.Templating, 2.0.14"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Meziantou.Framework.Templating@2.0.14
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Meziantou.Framework.Templating&version=2.0.14
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Framework.Templating&version=2.0.14
                    
Install as a Cake Tool

Meziantou.Framework.Templating

A powerful and flexible .NET template engine that allows you to create text templates with embedded C# code. The template syntax is customizable, and code sections use C# for maximum flexibility and type safety.

Features

  • Embedded C# code - Write C# code directly in your templates
  • Customizable syntax - Configure the code block delimiters to match your needs
  • Dynamic compilation - Templates are compiled to IL for optimal performance
  • Type-safe parameters - Add strongly-typed or dynamic parameters to your templates
  • Custom output types - Use any output writer type for your templates
  • Using directives - Import namespaces and types for use in templates
  • Debug support - Generate debug symbols for template debugging

Usage

Basic Template

The simplest way to use a template is to load text and run it:

using Meziantou.Framework.Templating;

var template = new Template();
template.Load("Hello World!");
var result = template.Run();
// result: "Hello World!"

Templates with Parameters

Add parameters to make your templates dynamic:

var template = new Template();
template.Load("Hello <%=Name%>!");
template.AddArgument("Name", typeof(string));
var result = template.Run("Meziantou");
// result: "Hello Meziantou!"

Using Named Parameters

You can also use named parameters with dictionaries:

var template = new Template();
template.Load("Hello <%=Name%>!");
var arguments = new Dictionary<string, object>
{
    { "Name", "Meziantou" }
};
template.AddArguments(arguments);
var result = template.Run(arguments);
// result: "Hello Meziantou!"

Code Blocks

Templates support three types of code blocks:

1. Evaluation blocks (<%=...%>)

Evaluate expressions and write their result to the output:

var template = new Template();
template.Load("2 + 2 = <%= 2 + 2 %>");
var result = template.Run();
// result: "2 + 2 = 4"
2. Statement blocks (<%...%>)

Execute C# statements:

var template = new Template();
template.Load("Numbers: <% for(int i = 1; i <= 5; i++) { %><%= i %><% } %>");
var result = template.Run();
// result: "Numbers: 12345"
3. Mixed content

Combine text, evaluation blocks, and statement blocks:

var template = new Template();
template.Load("""
<% for(int i = 1; i <= 3; i++) { %>
  Item <%= i %>: <%= i * 10 %>
<% } %>
""");
var result = template.Run();
// result:
//   Item 1: 10
//   Item 2: 20
//   Item 3: 30

Advanced Usage

Custom Delimiters

Change the code block delimiters to match your preferred syntax:

var template = new Template
{
    StartCodeBlockDelimiter = "{{",
    EndCodeBlockDelimiter = "}}"
};
template.Load("Hello {{=Name}}!");
template.AddArgument("Name", typeof(string));
var result = template.Run("World");
// result: "Hello World!"

Adding Using Directives

Import namespaces to use types without fully-qualified names:

var template = new Template();
template.AddUsing("System.Linq");
template.Load("<%= Enumerable.Range(1, 5).Sum() %>");
var result = template.Run();
// result: "15"

You can also import types with aliases:

var template = new Template();
template.AddUsing(typeof(System.Text.StringBuilder), "SB");
template.Load("<% var sb = new SB(); sb.Append(\"Hello\"); %><%= sb.ToString() %>");
var result = template.Run();
// result: "Hello"

Custom Output Type

Use a custom output type for specialized scenarios:

var template = new Template();
template.OutputType = typeof(Output);
template.Load("<%__output__.Write(\"Custom output\");%>");
var result = template.Run();
// result: "Custom output"

Dynamic Parameters

Use dynamic parameters when types are not known at compile time:

var template = new Template();
template.Load("Value: <%=Value%>");
template.AddArgument("Value"); // No type specified = dynamic
var result = template.Run(42);
// result: "Value: 42"

Building Templates Separately

Templates can be built (compiled) separately from execution:

var template = new Template();
template.Load("Hello <%=Name%>");
template.AddArgument("Name", typeof(string));

// Compile the template
template.Build(CancellationToken.None);

// Run multiple times (compiled code is reused)
var result1 = template.Run("Alice");
var result2 = template.Run("Bob");

Debug Mode

Enable debug mode to generate debug symbols for troubleshooting:

var template = new Template
{
    Debug = true
};
template.Load("<%=Value%>");
template.AddArgument("Value", typeof(int));
var result = template.Run(42);

Accessing Generated Source Code

After building a template, you can inspect the generated C# source code:

var template = new Template();
template.Load("Hello <%=Name%>");
template.AddArgument("Name", typeof(string));
template.Build(CancellationToken.None);

Console.WriteLine(template.SourceCode);
// Prints the generated C# class

Template Syntax

The default template syntax uses <% and %> delimiters:

Syntax Description Example
<%= expression %> Evaluates an expression and writes it to output <%= 2 + 2 %>
<% statement %> Executes a C# statement <% var x = 10; %>
Text Any text outside code blocks is written as-is Hello World

Error Handling

The library throws TemplateException for template-related errors:

try
{
    var template = new Template();
    template.Load("<%=InvalidExpression%>");
    template.Build(CancellationToken.None);
}
catch (TemplateException ex)
{
    Console.WriteLine($"Template error: {ex.Message}");
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Meziantou.Framework.Templating:

Package Downloads
Meziantou.Framework.Templating.Html

HTML Template parser and evaluator. Provide a few syntax extensions to encode values or handle attached files for emails.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.14 151 3/15/2026
2.0.13 140 1/18/2026
2.0.12 195 11/23/2025
2.0.11 160 11/16/2025
2.0.10 169 11/2/2025
2.0.9 151 10/19/2025
2.0.8 246 9/3/2025
2.0.7 274 5/18/2025
2.0.6 263 3/1/2025
2.0.5 188 11/17/2024
2.0.4 174 6/24/2024
2.0.3 3,349 11/15/2023
2.0.2 787 7/14/2021
2.0.1 646 4/22/2021
2.0.0 795 9/24/2020
1.1.5 948 6/25/2020
1.1.4 982 10/23/2019
1.1.3 788 6/28/2019
1.1.2 1,264 10/21/2018
1.1.1 1,694 7/7/2018
Loading failed