Universal.Common.Text.Templating
1.1.1
dotnet add package Universal.Common.Text.Templating --version 1.1.1
NuGet\Install-Package Universal.Common.Text.Templating -Version 1.1.1
<PackageReference Include="Universal.Common.Text.Templating" Version="1.1.1" />
<PackageVersion Include="Universal.Common.Text.Templating" Version="1.1.1" />
<PackageReference Include="Universal.Common.Text.Templating" />
paket add Universal.Common.Text.Templating --version 1.1.1
#r "nuget: Universal.Common.Text.Templating, 1.1.1"
#:package Universal.Common.Text.Templating@1.1.1
#addin nuget:?package=Universal.Common.Text.Templating&version=1.1.1
#tool nuget:?package=Universal.Common.Text.Templating&version=1.1.1
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 | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net10.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.
Now targets netstandard2.0 and net10.0 instead of net8.0 only, for broader compatibility.