2

I am creating a web application that will need to send a variety of emails out to users. Instead of hard-coding the contents of the email in the app, I want to use a template stored on disk and replace tokens in it (ex. "Hello, %%FirstName%%!") with the actual data. I have some experience with creating XSLT templates, but since the data isn't naturally in XML format this may not be the best fit. Is there a better template tool in .NET?

Note that I prefer one built in to the language but I'd consider add-ons too.

Thanks,

Graham

Graham Powell
  • 283
  • 1
  • 12
  • I think there is none. Most of the time developer uses the tokens but replaces them with the String.Replace or StringBuilder.Replace kind of way. I think that makes sense because the only consideration to use xslt or any other solution is, how flexible are they to the changes? because that is the main thing to consider – Prashant Lakhlani Mar 06 '10 at 08:08
  • Check out Spark, great template tool in .NET – Noel Mar 09 '10 at 09:59

3 Answers3

2

I have personally use Spark. Its an easy to use text templating library (below is an example of Spark syntax)

<var names="new [] {'alpha', 'beta', 'gamma'}"/>
<for each="var name in names">
  <test if="name == 'beta'">
    <p>beta is my favorite.</p>
    <else/>
    <p>${name} is okay too I suppose. 
  </test>
</for>

It even comes with a sample code for email text templating which you could look at on github.

Noel
  • 593
  • 4
  • 15
1

You may want to take a look at MVCContrib. They have an e-mail template system IEmailTemplateService that uses Views to render the actual e-mail message which gives you access to a lot of features, including different view engines. If you are not in an MVC environment, you could possibly still extract some useful tricks out there.

mnemosyn
  • 45,391
  • 6
  • 76
  • 82
0

I am using NVelocity for that, and I find it's very flexible and easy to use. It not only allows you to replace tokes, but it also includes sort of a programming language, e.g. for conditionals (if) and loops (for, foreach).

The original project seems to be dead, but there is a fork maintained by the Castle project.

Of course there are lots of other, alternative template libraries (stringtemplate seems to be popular), as you can see in this question I asked some time ago.

Community
  • 1
  • 1
M4N
  • 94,805
  • 45
  • 217
  • 260