Meziantou.Framework.Templating.Html 3.0.2

Prefix Reserved
dotnet add package Meziantou.Framework.Templating.Html --version 3.0.2
                    
NuGet\Install-Package Meziantou.Framework.Templating.Html -Version 3.0.2
                    
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.Html" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.Templating.Html" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Framework.Templating.Html" />
                    
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.Html --version 3.0.2
                    
#r "nuget: Meziantou.Framework.Templating.Html, 3.0.2"
                    
#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.Html@3.0.2
                    
#: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.Html&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Framework.Templating.Html&version=3.0.2
                    
Install as a Cake Tool

Meziantou.Framework.Templating.Html

A .NET library for generating HTML emails using a template engine with built-in encoding and section support.

Usage

This library extends the Meziantou.Framework.Templating engine with HTML-specific features, making it easy to create dynamic HTML emails with proper encoding, sections for metadata (like email titles), and support for embedded content identifiers.

Basic Template

using Meziantou.Framework.Templating;

var template = new HtmlEmailTemplate();
template.Load("Hello {{# \"Meziantou\" }}!");

var result = template.Run(out var metadata);
// Output: Hello Meziantou!

Template Syntax

The template uses {{ and }} delimiters for code blocks. Here are the available directives:

Expression Evaluation (#)

Evaluate and output a C# expression:

var template = new HtmlEmailTemplate();
template.Load("Hello {{# userName }}!");

var result = template.Run(out _, new Dictionary<string, object?> { ["userName"] = "John" });
// Output: Hello John!
HTML Encoding (#html)

Encode HTML content to prevent XSS attacks:

var template = new HtmlEmailTemplate();
template.Load("Hello {{#html \"<Meziantou>\" }}!");

var result = template.Run(out _);
// Output: Hello &lt;Meziantou&gt;!
HTML Attribute Encoding (#attr)

Encode values for use in HTML attributes:

var template = new HtmlEmailTemplate();
template.Load("Hello <a href=\"{{#attr \"Sample&Sample\"}}\">Meziantou</a>!");

var result = template.Run(out _);
// Output: Hello <a href="Sample&amp;Sample">Meziantou</a>!
URL Encoding (#url)

Encode values for use in URLs:

var template = new HtmlEmailTemplate();
template.Load("Hello <a href=\"http://www.localhost.com/{{#url \"Sample&Url\" }}\">Meziantou</a>!");

var result = template.Run(out _);
// Output: Hello <a href="http://www.localhost.com/Sample%26Url">Meziantou</a>!
Content Identifier (cid)

Generate content identifiers for embedded resources in emails:

var template = new HtmlEmailTemplate();
template.Load("<img src=\"{{cid logo.png}}\" />");

var result = template.Run(out var metadata);
// Output: <img src="cid:logo.png" />
// metadata.ContentIdentifiers contains ["logo.png"]
HTML Code Blocks (html)

Write HTML-encoded C# code (useful when your template contains HTML entities):

var template = new HtmlEmailTemplate();
template.Load("{{html for(int i = 0; i &lt; 3; i++) { }}{{#i}} {{ } }}");

var result = template.Run(out _);
// Output: 0 1 2

Sections

Sections allow you to capture parts of the template output for use as metadata (e.g., email subject):

var template = new HtmlEmailTemplate();
template.Load("Hello {{@begin_section title}}{{# \"Meziantou\" }}{{@end_section}}!");

var result = template.Run(out var metadata);
// Output: Hello Meziantou!
// metadata.Title: "Meziantou"

Metadata

The HtmlEmailMetadata class contains extracted information from the template:

  • Title - Content from the "title" section (typically used for email subject)
  • ContentIdentifiers - List of content identifiers (CIDs) referenced in the template
var template = new HtmlEmailTemplate();
template.Load(@"
{{@begin_section title}}Welcome Email{{@end_section}}
<html>
  <body>
    <img src=""{{cid logo.png}}"" />
    <h1>Hello {{#html userName}}!</h1>
  </body>
</html>");

var result = template.Run(out var metadata,
    new Dictionary<string, object?> { ["userName"] = "John" });

// metadata.Title: "Welcome Email"
// metadata.ContentIdentifiers: ["logo.png"]

Passing Parameters

You can pass parameters to templates in multiple ways:

// Using a dictionary
var result = template.Run(out var metadata,
    new Dictionary<string, object?> { ["name"] = "John", ["age"] = 30 });

// Using positional parameters
var result = template.Run(out var metadata, "John", 30);

// No parameters
var result = template.Run(out var metadata);
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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 101 6/7/2026
3.0.1 95 5/31/2026
3.0.0 94 5/30/2026
2.0.24 99 5/14/2026
2.0.23 111 4/25/2026
2.0.22 125 3/29/2026
2.0.21 117 3/15/2026
2.0.20 108 2/22/2026
2.0.19 122 1/18/2026
2.0.18 179 12/14/2025
2.0.17 171 11/23/2025
2.0.16 157 11/16/2025
2.0.15 162 11/2/2025
2.0.14 153 10/19/2025
2.0.13 345 9/16/2025
2.0.12 232 9/3/2025
2.0.11 166 8/10/2025
2.0.10 176 7/13/2025
2.0.9 247 6/15/2025
2.0.8 177 5/18/2025
Loading failed