Simple and flexible object-to-template string mapper with formatting support
ObjectSemantics.NET is a lightweight C# library that lets you inject object property values directly into string templates much like Handlebars or Helm templates, but focused on .NET.
This is especially useful when you want to dynamically generate content such as:
- Email templates
- HTML fragments
- Reports or invoices
- Config files
- Logging output
Install from NuGet:
Install-Package ObjectSemantics.NETPerson person = new Person
{
Name = "John Doe"
};
// Define template and map it using the object
string result = person.Map("I am {{ Name }}!");
Console.WriteLine(result);Output:
I am John Doe!
Person person = new Person
{
Name = "Jane Doe"
};
// You can also start with the string template
string result = "I am {{ Name }}!".Map(person);
Console.WriteLine(result);Output:
I am Jane Doe!
Person person = new Person
{
MyCars = new List<Car>
{
new Car { Make = "BMW", Year = 2023 },
new Car { Make = "Rolls-Royce", Year = 2020 }
}
};
string template = @"
{{ #foreach(MyCars) }}
- {{ Year }} {{ Make }}
{{ #endforeach }}";
string result = person.Map(template);
Console.WriteLine(result);Output:
- 2023 BMW
- 2020 Rolls-Royce
Person person = new Person
{
Age = 40
};
string template = @"
{{ #if(Age >= 18) }}
Adult
{{ #else }}
Minor
{{ #endif }}";
string result = person.Map(template);
Console.WriteLine(result);Output:
Adult
Car car = new Car
{
Price = 50000
};
string result = car.Map("{{ Price:#,##0 }} | {{ Price:N2 }}");
Console.WriteLine(result);Output:
50,000 | 50,000.00
Explore more usage examples and edge cases in the Wiki Page:
Feel free to open issues or contribute improvements via pull requests!