views:

89

answers:

2

I have a application that generates a couple of different mails. These mails are currently build up using a string builder that generates a HTML based string that is the mail content.

This approach is getting messy. The code is objects mixed with HTML, etc, etc. What I'd like is to have a template similar to the one used in for example MVC and then use the output of this template to add to the mail.

Can this be done using for example T4 add how wound that work or should I use another approach for this?

The templates don't have to be editable at runtime even if that would be nice.

+2  A: 

It can be done with T4. It would work exactly the way it does in MVC.

You should consider getting the Clarius T4 Editor, as Visual Studio does not come with intellisense for T4 out of the box, and the Clarius T4 editor can provide that.

Robert Harvey
Any good examples on how to feed a template with an object and then use that object in the template and return the generated string?
Riri
TextTransform.exe is the command line tool that does this. See http://msdn.microsoft.com/en-us/library/bb126461.aspx
Robert Harvey
Scott Hanselman gives a pretty good demonstration of this in his NerdDinner talk at http://videos.visitmix.com/MIX09/T49F. The discussion about T4 templates begins at 23:10 (23 minutes ten seconds into the talk).
Robert Harvey
There are some good tutorials here: http://www.olegsych.com/2007/12/text-template-transformation-toolkit/
Robert Harvey
+1  A: 

Have you considered just using XSLT? You can easily create HTML documents from raw XML by applying an XSL Transform in .Net

We store XSL templates in the database, and use a simple NAME/VALUE pair approach to populate the templates in code. It is very effective and flexible, and the output can be piped to the browser easily. Alternatively you could just store the individual templates in files on the server and load them up that way.

Either way, the amount of code to implement this is relatively simple, and .Net has many classes in place to facilitate this. If you want an example of how we implemented this, leave a comment and I will append some code examples to this answer.

Josh
Yes, please. I would like to see the samples.
Robert Harvey