Skip to content

swagfin/ObjectSemantics.NET

Repository files navigation

ObjectSemantics.NET

FOSSA Status

Simple and flexible object-to-template string mapper with formatting support

🧠 Overview

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

📦 Installation

Install from NuGet:

Install-Package ObjectSemantics.NET

🚀 Quick Start

Example 1: Mapping Object Properties

Person 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!

Example 2: Mapping Using String Extension

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!

Example 3: Mapping Enumerable Collections (Looping)

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

Example 4: Conditional Logic with #if, #else, and #endif

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

Example 5: Number Formatting Support

Car car = new Car
{
    Price = 50000
};

string result = car.Map("{{ Price:#,##0 }} | {{ Price:N2 }}");

Console.WriteLine(result);

Output:

50,000 | 50,000.00

💡 More Examples & Documentation

Explore more usage examples and edge cases in the Wiki Page:

📁 Wiki Page


🤝 Contributing

Feel free to open issues or contribute improvements via pull requests!


📄 MIT License

FOSSA Status

About

Simple Object to File Mapper that supports string formatting

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages