Universal.Common.Text.Templating
1.1.0
See the version list below for details.
dotnet add package Universal.Common.Text.Templating --version 1.1.0
NuGet\Install-Package Universal.Common.Text.Templating -Version 1.1.0
<PackageReference Include="Universal.Common.Text.Templating" Version="1.1.0" />
<PackageVersion Include="Universal.Common.Text.Templating" Version="1.1.0" />
<PackageReference Include="Universal.Common.Text.Templating" />
paket add Universal.Common.Text.Templating --version 1.1.0
#r "nuget: Universal.Common.Text.Templating, 1.1.0"
#:package Universal.Common.Text.Templating@1.1.0
#addin nuget:?package=Universal.Common.Text.Templating&version=1.1.0
#tool nuget:?package=Universal.Common.Text.Templating&version=1.1.0
Universal.Common.Text.Templating
A flexible and extensible text templating system that replaces placeholders in templates with values from objects.
Classes
TextTemplateRenderer
Renders text templates by replacing placeholders with provided values from object properties.
// Basic usage with default options ({{Name}} syntax)
var renderer = new TextTemplateRenderer();
string template = "Hello {{Name}}, welcome to {{City}}!";
var values = new { Name = "Alice", City = "Melbourne" };
string result = renderer.Render(template, values);
// Output: "Hello Alice, welcome to Melbourne!"
// Custom configuration
var renderer = new TextTemplateRenderer(new TextTemplateRenderer.Options
{
RequireAllParameters = false,
NullValueReplacement = "N/A",
PlaceholderFormat = new DollarBracesPlaceholderFormat(),
ValueFormatter = value => System.Web.HttpUtility.HtmlEncode(value.ToString())
});
Configuration Options
- RequireAllParameters: When true, throws an exception if any placeholder lacks a corresponding property (default: true)
- NullValueReplacement: String to use when a property value is null (default: empty string)
- PlaceholderFormat: The placeholder syntax format to use (default: DoubleBracesPlaceholderFormat)
- ValueFormatter: Custom function to format values before replacement (useful for HTML encoding, JSON escaping, etc.)
- EnableRecursiveResolution: When true, if a replacement value itself contains placeholders, those are resolved too, repeating until the result stops changing or
MaxRecursionDepthis reached (default: false) - MaxRecursionDepth: Maximum number of extra passes performed when
EnableRecursiveResolutionis true; guards against circular references (e.g.Aresolves to{{B}}andBresolves to{{A}}). Set tonullfor unbounded recursion. (default: 10)
// Recursive resolution: values can reference other placeholders
var renderer = new TextTemplateRenderer(new TextTemplateRenderer.Options
{
EnableRecursiveResolution = true
});
string template = "Hello {{Name}}";
var values = new { Name = "{{Greeting}}", Greeting = "World" };
string result = renderer.Render(template, values);
// Output: "Hello World"
When RequireAllParameters is true and EnableRecursiveResolution is true, a placeholder introduced by a value that has no matching property (e.g. Name resolves to {{Unknown}} and there is no Unknown property) also throws, just like a missing top-level parameter. Without recursion enabled, such text is left as-is and is never treated as an error, since it's just data that happens to look like a placeholder.
DoubleBracesPlaceholderFormat
Placeholder format using {{Name}} syntax, similar to Mustache and Handlebars templates.
var format = new DoubleBracesPlaceholderFormat();
var renderer = new TextTemplateRenderer(new TextTemplateRenderer.Options
{
PlaceholderFormat = format
});
string template = "User: {{Username}}, Email: {{Email}}";
DollarBracesPlaceholderFormat
Placeholder format using ${Name} syntax, common in shell scripts and many templating systems.
var format = new DollarBracesPlaceholderFormat();
var renderer = new TextTemplateRenderer(new TextTemplateRenderer.Options
{
PlaceholderFormat = format
});
string template = "User: ${Username}, Email: ${Email}";
IPlaceholderFormat
Interface for implementing custom placeholder formats. Implement this interface to support alternative placeholder syntaxes.
public class CustomPlaceholderFormat : IPlaceholderFormat
{
public IEnumerable<PlaceholderMatch> Find(string text)
{
// Find all placeholders in the text
}
public string Replace(string text, string placeholderName, string value)
{
// Replace placeholders with values
}
}
PlaceholderMatch
Represents a matched placeholder in a template, including its position and extracted name.
// Properties:
// - FullMatch: The complete matched text including delimiters (e.g., "{{Name}}")
// - Name: The placeholder name without delimiters (e.g., "Name")
// - Index: Starting position of the match in the text
// - Length: Length of the full match
| Product | Versions 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 was computed. 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 was computed. 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. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added opt-in recursive placeholder resolution (EnableRecursiveResolution/MaxRecursionDepth on Options) so a replacement value that itself contains a placeholder gets resolved too, with a depth cap guarding against circular references. RequireAllParameters now also validates recursively-introduced placeholders when recursion is enabled. Replacement is now a single order-independent pass per round, so the result no longer depends on reflected property order.