Skip to content

Commit 382622d

Browse files
authored
Update README.md
1 parent 05354c2 commit 382622d

1 file changed

Lines changed: 73 additions & 38 deletions

File tree

README.md

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ Simple Object to File Mapper that supports string formatting
55

66
## Overview
77

8-
* Mapping your object e.g. StudentDetails to a html Template that you define
9-
* Mapping a collection to enumerable or List to a html/text/ (predefined format)
8+
* Maps properties from a source object to a template string and returns the result. This is useful for dynamically generating strings based on object properties.
109

1110
## Install
1211

@@ -16,51 +15,87 @@ Install-Package ObjectSemantics.NET
1615
```
1716
https://nuget.org/packages/ObjectSemantics.NET
1817

19-
**USAGE**
18+
**USAGE (Example 1)**
2019
```cs
21-
using ObjectSemantics.NET;
22-
class Program
20+
// Create Model
21+
Student student = new Student
2322
{
24-
static void Main(string[] args)
25-
{
26-
27-
Student student = new Student
28-
{
29-
StudentName = "George",
30-
Invoices = new List<Invoice>
31-
{
32-
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000 },
33-
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320 }
34-
}
35-
};
23+
StudentName = "George Waynne",
24+
Balance = 2510
25+
};
3626

27+
// Define Template
28+
var template = new ObjectSemanticsTemplate
29+
{
30+
FileContents = @"My Name is: {{ StudentName }} and my balance is {{ Balance:N2 }}"
31+
};
3732

38-
ObjectSemanticsTemplate template = new ObjectSemanticsTemplate
39-
{
40-
FileContents = @"<h6>{{ StudentName:uppercase }} Invoices</h6>
41-
<ol>
42-
{{ #foreach(invoices) }}
43-
<li>Invoice No: {{ RefNo }} of {{ Narration }} amount {{ Amount:N0 }} </li>
44-
{{ #endforeach }}
45-
</ol>"
46-
};
33+
// Map Object to Template
34+
string generatedTemplate = TemplateMapper.Map(student, template);
4735

48-
string htmlWithData = TemplateMapper.Map(student, template);
36+
// Output the result
37+
Console.WriteLine(generatedTemplate);
38+
```
39+
***Output***
40+
```console
41+
My Name is: George Waynne and my balance is 2,510.00
42+
```
4943

50-
Console.WriteLine(htmlWithData);
44+
**USAGE (Example 2)**
5145

52-
Console.ReadLine();
53-
Environment.Exit(0);
54-
}
55-
}
46+
```csharp
47+
// Create Model
48+
Student student = new Student
49+
{
50+
StudentName = "John Doe",
51+
Invoices = new List<Invoice>
52+
{
53+
new Invoice{ Id = 2, RefNo = "INV_002", Narration = "Grade II Fees Invoice", Amount = 2000, InvoiceDate = new DateTime(2023, 04, 01) },
54+
new Invoice{ Id = 1, RefNo = "INV_001", Narration = "Grade I Fees Invoice", Amount = 320, InvoiceDate = new DateTime(2022, 08, 01) }
55+
}
56+
};
5657

57-
class Student
58+
// Define Template
59+
var template = new ObjectSemanticsTemplate
5860
{
59-
public Guid Id { get; set; } = Guid.NewGuid();
60-
public string StudentName { get; set; }
61-
public double Balance { get; set; }
62-
public DateTime RegDate { get; set; } = DateTime.Now;
63-
}
61+
FileContents = @"{{ StudentName }} Invoices
62+
{{ #foreach(Invoices) }}
63+
<tr>
64+
<td>{{ Id }}</td>
65+
<td>{{ RefNo }}</td>
66+
<td>{{ Narration }}</td>
67+
<td>{{ Amount:N0 }}</td>
68+
<td>{{ InvoiceDate:yyyy-MM-dd }}</td>
69+
</tr>
70+
{{ #endforeach }}"
71+
};
72+
73+
// Map Object to Template
74+
string generatedTemplate = TemplateMapper.Map(student, template);
75+
76+
// Output the result
77+
Console.WriteLine(generatedTemplate);
78+
```
79+
***Output***
80+
```console
81+
John Doe Invoices
82+
83+
<tr>
84+
<td>2</td>
85+
<td>INV_002</td>
86+
<td>Grade II Fees Invoice</td>
87+
<td>2,000</td>
88+
<td>2023-04-01</td>
89+
</tr>
90+
91+
<tr>
92+
<td>1</td>
93+
<td>INV_001</td>
94+
<td>Grade I Fees Invoice</td>
95+
<td>320</td>
96+
<td>2022-08-01</td>
97+
</tr>
98+
6499
```
65100

66101
## Check out more samples

0 commit comments

Comments
 (0)