MbSoftLab.TemplateEngine.Core 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package MbSoftLab.TemplateEngine.Core --version 1.0.5
NuGet\Install-Package MbSoftLab.TemplateEngine.Core -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MbSoftLab.TemplateEngine.Core" Version="1.0.5" />
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.5
#r "nuget: MbSoftLab.TemplateEngine.Core, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.5

// Install MbSoftLab.TemplateEngine.Core as a Cake Tool
#tool nuget:?package=MbSoftLab.TemplateEngine.Core&version=1.0.5

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.

picture of what can done by the TemplateEngine

 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> 

Install Package

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

PM> Install-Package MbSoftLab.TemplateEngine.Core

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..

Repository

https://bitbucket.org/MbSoftLab/mbsoftlab.templateengine/src/master/


Issues

report an issue


License

Code licensed under MIT

Documentation licensed under CC BY 4.0.

Product 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. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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.9 2,511 12/16/2023
1.0.8-preview2 1,811 1/1/2021
1.0.8-preview 363 12/25/2020
1.0.7 905 12/25/2020
1.0.7-preview 251 12/13/2020
1.0.5 600 11/8/2020
1.0.4 669 4/26/2020