HtmlEmailTemplate Class

Provides a templating engine specialized for generating HTML email content with automatic encoding and metadata extraction.

public class HtmlEmailTemplate : Template

Example

using var template = new HtmlEmailTemplate();
template.Load("<title>{{@begin_section title}}Welcome{{@end_section}}</title><h1>Hello {{#html userName}}!</h1>");
template.Arguments.Add(new TemplateArgument("userName", typeof(string)));

var result = template.Run(out var metadata, new Dictionary<string, object?> { ["userName"] = "John" });
Assert.Equal("<title>Welcome</title><h1>Hello John!</h1>", result);
Assert.NotNull(metadata);
Assert.Equal("Welcome", metadata.Title);
Inheritance
Template HtmlEmailTemplate

Constructors

NameDescription
HtmlEmailTemplate()

HtmlEmailTemplate()

HtmlEmailTemplate.cs
public HtmlEmailTemplate()

No documentation is available.

Methods

NameDescription
CreateCodeBlock(string, int)Creates an HTML-specific code block that supports HTML encoding, URL encoding, sections, and content identifiers.
CreateDirectiveBlock(string, string, string, int)
CreateOutput(TextWriter)Creates an HTML email output writer with encoding capabilities and section support.
InvokeRunMethod(object?[])Runs the template, then rejects it if it left a section open.
Run(HtmlEmailMetadata?)Runs the template without parameters and returns the generated HTML along with extracted metadata.
Run(HtmlEmailMetadata?, IDictionary<string, object?>)Runs the template with named parameters and returns the generated HTML along with extracted metadata.
Run(HtmlEmailMetadata?, object?[])Runs the template with positional parameters and returns the generated HTML along with extracted metadata.
Run(TextWriter, HtmlEmailMetadata?)Runs the template without parameters and writes the generated HTML to the specified writer.
Run(TextWriter, HtmlEmailMetadata?, IReadOnlyDictionary<string, object?>)Runs the template with named parameters and writes the generated HTML to the specified writer.
Run(TextWriter, HtmlEmailMetadata?, object?[])Runs the template with positional parameters and writes the generated HTML to the specified writer.

CreateCodeBlock(string, int)

HtmlEmailTemplate.cs

Creates an HTML-specific code block that supports HTML encoding, URL encoding, sections, and content identifiers.

protected override CodeBlock CreateCodeBlock(string text, int index)

Parameters

text string
index int

Returns

CodeBlock

CreateDirectiveBlock(string, string, string, int)

HtmlEmailTemplate.cs
protected override DirectiveBlock CreateDirectiveBlock(string text, string name, string value, int index)

Parameters

text string
name string
value string
index int

Returns

DirectiveBlock

No documentation is available.

CreateOutput(TextWriter)

HtmlEmailTemplate.cs

Creates an HTML email output writer with encoding capabilities and section support.

protected override object CreateOutput(TextWriter writer)

Parameters

writer TextWriter

Returns

object

InvokeRunMethod(object?[])

HtmlEmailTemplate.cs

Runs the template, then rejects it if it left a section open.

protected override void InvokeRunMethod(object?[] p)

Parameters

p object?[]

Exceptions

TemplateException
The template started a section but never ended it.

Run overloads

Run(HtmlEmailMetadata?)

HtmlEmailTemplate.cs

Runs the template without parameters and returns the generated HTML along with extracted metadata.

public virtual string Run(out HtmlEmailMetadata? metadata)
Parameters
metadata HtmlEmailMetadata?
Receives metadata extracted from the template, including title and content identifiers.
Returns

string The generated HTML content.

Run(HtmlEmailMetadata?, IDictionary<string, object?>)

HtmlEmailTemplate.cs

Runs the template with named parameters and returns the generated HTML along with extracted metadata.

public virtual string Run(out HtmlEmailMetadata? metadata, IDictionary<string, object?> parameters)
Parameters
metadata HtmlEmailMetadata?
Receives metadata extracted from the template, including title and content identifiers.
parameters IDictionary<string, object?>
Named parameters to pass to the template.
Returns

string The generated HTML content.

Run(HtmlEmailMetadata?, object?[])

HtmlEmailTemplate.cs

Runs the template with positional parameters and returns the generated HTML along with extracted metadata.

public virtual string Run(out HtmlEmailMetadata? metadata, params object?[] parameters)
Parameters
metadata HtmlEmailMetadata?
Receives metadata extracted from the template, including title and content identifiers.
parameters object?[]
Positional parameters to pass to the template.
Returns

string The generated HTML content.

Run(TextWriter, HtmlEmailMetadata?)

HtmlEmailTemplate.cs

Runs the template without parameters and writes the generated HTML to the specified writer.

public virtual void Run(TextWriter writer, out HtmlEmailMetadata? metadata)
Parameters
writer TextWriter
The text writer to write the generated HTML to.
metadata HtmlEmailMetadata?
Receives metadata extracted from the template, including title and content identifiers.

Run(TextWriter, HtmlEmailMetadata?, IReadOnlyDictionary<string, object?>)

HtmlEmailTemplate.cs

Runs the template with named parameters and writes the generated HTML to the specified writer.

public virtual void Run(TextWriter writer, out HtmlEmailMetadata? metadata, IReadOnlyDictionary<string, object?> parameters)
Parameters
writer TextWriter
The text writer to write the generated HTML to.
metadata HtmlEmailMetadata?
Receives metadata extracted from the template, including title and content identifiers.
parameters IReadOnlyDictionary<string, object?>
Named parameters to pass to the template.

Run(TextWriter, HtmlEmailMetadata?, object?[])

HtmlEmailTemplate.cs

Runs the template with positional parameters and writes the generated HTML to the specified writer.

public virtual void Run(TextWriter writer, out HtmlEmailMetadata? metadata, params object?[] parameters)
Parameters
writer TextWriter
The text writer to write the generated HTML to.
metadata HtmlEmailMetadata?
Receives metadata extracted from the template, including title and content identifiers.
parameters object?[]
Positional parameters to pass to the template.