Introduction
Avalanche.Template contains classes for text templating, [git], [www].
Add package reference to .csproj.
<PropertyGroup>
<RestoreAdditionalProjectSources>https://avalanche.fi/Avalanche.Core/nupkg/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalanche.Template"/>
</ItemGroup>
TemplateText(text, format) represents a template text.
ITemplateText templateText = new TemplateText("Error code {0} (0x{0:X4}).", TemplateFormat.Brace).SetReadOnly();
WriteLine(templateText.Print(null, new object?[] { 0x100 })); // "Error code 256 (0x0100)."
TemplateFormat.Brace detects which parameter convention to use, numeric "{0}" or alphanumeric "{user}".
ITemplateFormat templateFormat = TemplateFormat.Brace;
ITemplateBreakdown breakdown2 = templateFormat.Breakdown["Today is {time}. Welcome, {user}"];
.Print(arguments) prints template string by placing arguments into placeholders.
// Create text without template format
ITemplatePrintable printable = new TemplateText("Time is {0}.", TemplateFormat.Brace).WithFormat(CultureInfo.CurrentCulture);
// Create arguments
object?[] arguments = { DateTime.Now };
// Print ok
string print = printable.Print(arguments);
// ""
WriteLine(print);
Class libraries:
- Avalanche.Template.dll contains implementations.
- Avalanche.Template.Abstractions.dll contains interfaces.
Dependency libraries:
- Avalanche.Utilities.dll
- Avalanche.Utilities.Abstractions.dll
The following GlobalUsings.cs can be used to include all extension methods.
global using Avalanche.Template;
global using Avalanche.Utilities;
global using Avalanche.Utilities.Provider;
Full Example
Full example
using System.Globalization;
using Avalanche.Template;
using Avalanche.Utilities;
using static System.Console;
class templateindex
{
public static void Run()
{
{
// <01>
ITemplateText templateText = new TemplateText("Error code {0} (0x{0:X4}).", TemplateFormat.Brace).SetReadOnly();
WriteLine(templateText.Print(null, new object?[] { 0x100 })); // "Error code 256 (0x0100)."
// </01>
}
{
// <02>
ITemplateFormat templateFormat = TemplateFormat.Brace;
ITemplateBreakdown breakdown2 = templateFormat.Breakdown["Today is {time}. Welcome, {user}"];
// </02>
}
{
// <03>
// Create text without template format
ITemplatePrintable printable = new TemplateText("Time is {0}.", TemplateFormat.Brace).WithFormat(CultureInfo.CurrentCulture);
// Create arguments
object?[] arguments = { DateTime.Now };
// Print ok
string print = printable.Print(arguments);
// ""
WriteLine(print);
// </03>
}
}
}