Skip to content
mbsoftlab edited this page Jan 2, 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

  Person person = new Person
  {
      FirstName = "Jo",
      Emails = new List<string>(){"[email protected]","[email protected]","[email protected]"}
  };
  
  ITemplateEngine<Person> templateEngine = new RazorTemplateEngine<Person>();
  templateEngine.LoadTemplateFromFile<Person>("PersonEmailTemplate.cshtml");
  string templateFileContent = templateEngine.CreateStringFromTemplate(person);
  Console.WriteLine();
  Console.WriteLine(templateFileContent);
  
  var htmlFileName = Path.Combine(Path.GetTempPath(), "temp.html");
  File.WriteAllText(htmlFileName, templateFileContent);

Person.cs

namespace TemplateEngineVisualStudioCodeExample
{
    public class Person: TemplateDataModel<Person>
    {
        public string FirstName { get; set; }
        public List<string> Emails { get; set; }
    }
}

PersonEmailTemplate.cshtml

@inherits TemplateEngineVisualStudioCodeExample.Person

<p>Hallo @Model.FirstName. This are your Emails:</p>
@foreach (var email in @Model.Emails)
{
    <p>email</p>
}

Install Package

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

PM> Install-Package MbSoftLab.TemplateEngine.Core

Clone this wiki locally