Skip to content
mbsoftlab edited this page Jan 1, 2021 · 15 revisions

QuickStart

The TemplateEngine replaces values from properties of C# classes in template strings. The C# class with the data holding properties is called the TemplateDataModel class. The string with the placeholders is called a string template or template string.

You can bind the value from your C-property to your string template by using ${YourPropertyName}. You can set a custom delimiters. Use the OpeningDelimiter and CloseingDelimiter properties to handle this.

Also you can access parameterless public methods of TemplateDataModell classes by using ${MethodName()} in your template.

The default is ${ for the start delimiter and } for the end delimiter.

Since Version 1.0.8 the Razor language is suporrted by RazorTemplateEngine.

'TemplateEngine' Example

 Person person = new Person
 {
     FirstName = "Jo",
     LastName="Doe"
 };

string template = "<MyTag>${FirstName}, ${LastName}</MyTag>";

TemplateEngine templateEngine = new TemplateEngine(person,template);
string outputString = templateEngine.CreateStringFromTemplate();

Console.Write(outputString); // Output: <MyTag>Jo, Doe</MyTag> 

'RazorTemplateEngine' Example

Programm.cs

public class Person : TemplateDataModel<Person>
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

 Person person = new Person
 {
     FirstName = "Jo",
     LastName="Doe"
 };

ITemplateEngine<Person> templateEngine = new RazorTemplateEngine<Person>();
templateEngine.LoadTemplateFromFile<Person>("TestModel.cshtml");
string templateFileContent = templateEngine.CreateStringFromTemplate(testModel);
Console.WriteLine();
Console.WriteLine(templateFileContent);

var htmlFileName = Path.Combine(Path.GetTempPath(), "temp.html");
File.WriteAllText(htmlFileName, templateFileContent);

Install Package

NuGet Package: https://www.nuget.org/packages/MbSoftLab.TemplateEngine.Core/

PM> Install-Package MbSoftLab.TemplateEngine.Core

Clone this wiki locally