0

In short, I'd like to have a template that looks something like:

Dear @model.Firstname @model.Surname

I'd like to feed that into a method together with a prepopulated model like:

private String Transform(String Template, object model)  
{

}

I then want to find all the places in the template starting with @ and replace it with the data contained in the supplied model.

The model would look something like:

    public class Receipt
    {
        public String Firstname { get; set; }
        public String Surname { get; set; }
        ...

I'm sure I should be able to get this done using reflection. How do I load the model object as a type of Model and then access the data it holds?

They do it in Razor, so I assume it must be possible.

Ivo
  • 3,406
  • 4
  • 33
  • 56
Gineer
  • 2,358
  • 4
  • 26
  • 40
  • 1
    You can use Razor itself. [This post](http://stackoverflow.com/questions/4368815/razor-views-as-email-templates) should help. – bluevector Jun 15 '12 at 13:14

2 Answers2

0

Take a look at MvcMailer, which uses Razor views to compose emails.

jrummell
  • 42,637
  • 17
  • 112
  • 171
0

Okay, so the answer to this is to take a look at the RazorEngine over at codeplex.

  string template = "Hello @Model.Name! Welcome to Razor!";  
  string result = Razor.Parse(template, new { Name = "World" });

It really is that easy.

Gineer
  • 2,358
  • 4
  • 26
  • 40