MbSoftLab.TemplateEngine.Core 1.0.4

.NET Core 2.1
There is a newer version of this package available.
See the version list below for details.
Install-Package MbSoftLab.TemplateEngine.Core -Version 1.0.4
dotnet add package MbSoftLab.TemplateEngine.Core --version 1.0.4
<PackageReference Include="MbSoftLab.TemplateEngine.Core" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MbSoftLab.TemplateEngine.Core --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MbSoftLab.TemplateEngine.Core, 1.0.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MbSoftLab.TemplateEngine.Core as a Cake Addin
#addin nuget:?package=MbSoftLab.TemplateEngine.Core&version=1.0.4

// Install MbSoftLab.TemplateEngine.Core as a Cake Tool
#tool nuget:?package=MbSoftLab.TemplateEngine.Core&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

MbSoftLab.TemplateEngine.Core

namespace MbSoftLab.TemplateEngine.Core
{
    public class TemplateEngine : TemplateEngine<object>{}

    public class TemplateEngine<T>{...}
}

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.

 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> 

Methods

Methodname Description
string CreateStringFromTemplate([string template]) Creates a String from Datamodell and Template
void LoadTemplateFromFile(string filename) Loads a Stringtemplate from file.
TemplateEngine() Constructor
TemplateEngine(object templateDataModel, string stringTemplate) Constructor
TemplateEngine(object templateDataModel) Constructor
TemplateEngine<T>() Constructor
TemplateEngine<T>(T templateDataModel, string stringTemplate) Constructor
TemplateEngine<T>(T templateDataModel) Constructor

Propertys

Propertyname Datatype Description
OpeningDelimiter String Set the beginning delimiter for propertyreplacement
CloseingDelimiter String Set the ending delimiter for propertyreplacement
TemplateDataModel Generic / object Modell with Properys for Dataholding
TemplateString string Templatestring
NullStringValue string String for NULL-Values

Exampels

Load the template and fill with Data from modell

// Create a modell Class for Data
 TemplateDataModel templateDataModel = new TemplateDataModel
 {
     ProjektName = "Projektname"
 };

string template = "<MyTag>${ProjektName}</MyTag>";

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

Console.Write(outputString); // Ausgabe: <MyTag>Projektname</MyTag> 

Load template from file

    TemplateDataModel templateDataModel = new TemplateDataModel
    {
      ProjektName = "Projektname",
      KundeNummer = "1234",
      ProjektLink = "https://google.de",
      ProjektKuerzel = "PKZL"
    };

     TemplateEngine templateEngine = new TemplateEngine(templateDataModel);
     templateEngine.LoadTemplateFromFile("Html.template.html");
     string outputString = templateEngine.CreateStringFromTemplate();

     Console.WriteLine(outputString);

Template and model over PropertyInjection

 TemplateDataModel templateDataModel = new TemplateDataModel
 {
     ProjectName = "Projectname",
     KundeNummer = "1234",
     ProjektLink = "https://google.de",
     ProjektKuerzel = "PKZL"
 };

 string template = "<p>${ProjectName}</p>";
 TemplateEngine templateEngine = new TemplateEngine();
 templateEngine.TemplateDataModel = templateDataModel;
 templateEngine.TemplateString = template;
 Console.WriteLine(templateEngine.CreateStringFromTemplate());

Template and model over DependencyInjection


  TemplateDataModel templateDataModel = new TemplateDataModel
  {
      ProjectName = "Projectname",
      KundeNummer = "1234",
      ProjektLink = "https://google.de",
      ProjektKuerzel = "PKZL"
  };

  string template = "<p>${ProjectName}</p>";
  TemplateEngine templateEngine = new TemplateEngine(templateDataModel,template);
  Console.WriteLine(templateEngine.CreateStringFromTemplate());


TemplateEngine with PropertyInjection and generic type

 TemplateDataModel templateDataModel = new TemplateDataModel
 {
     ProjectName = "Projectname",
     KundeNummer = null,
     ProjektLink = "https://google.de",
     ProjektKuerzel = "KZ"
 };

 string template = "<p>{{ProjectName}}</p><p>{{KundeNummer}}</p>";
 TemplateEngine<TemplateDataModel> templateEngine = new TemplateEngine<TemplateDataModel>()
 {
     TemplateDataModel = templateDataModel,
     TemplateString = template,
     OpeningDelimiter = "{{",
     CloseingDelimiter = "}}",
     NullStringValue = "???"
 };
 Console.WriteLine(templateEngine.CreateStringFromTemplate());


Datatype compatibility

  • ✔ String
  • ✔ Byte
  • ✔ Short
  • ✔ UShort
  • ✔ Long
  • ✔ ULong
  • ✔ SByte
  • ✔ Char
  • ✔ UInt16
  • ✔ Int32
  • ✔ UInt64
  • ✔ Int16
  • ✔ Int32
  • ✔ Int64
  • ✔ Decimal
  • ✔ Double
  • ✔ DateTime
  • ✔ Boolean
  • ❌ Object
  • ❌ CustomClasses
  • ❌ IList, List, Dictionary, IEnumerable, etc..

Issues

report an issue

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MbSoftLab.TemplateEngine.Core:

Package Downloads
MbSoftLab.SwaggerUiHeaderBuilder

Der SwaggerUiHeaderBuilder erstellt einen Spezial-Header für das Swagger Ui

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8-preview2 1,704 1/1/2021
1.0.8-preview 285 12/25/2020
1.0.7 578 12/25/2020
1.0.7-preview 163 12/13/2020
1.0.5 411 11/8/2020
1.0.4 441 4/26/2020