views:

172

answers:

2

I’ve hit the need to be able to store templatized content separately from the code that generates the dynamic fields that fill that content (think mail merge in word). The use case generally revolves around the fact that I want to write code that can send out some type of dynamic content (often an email) which I want to allow my users to customize without changing any code.

For example, we send emails to our clients. We end up tweaking the emails that go out and adding dynamic content (i.e. for example we may want to say “Dear {Contact.FirstName},”). It’s convenient to throw the format of these emails into the database or some XML file and write the code that gives it the values of all the properties separately. So create an Email.XML with the content and an engine that takes that XML file passes it the Contact object and returns the HTML of the email. Conceptually this fairly similar to the goal of separating the view layer in the MVC pattern (the email is the view in this case, the engine is the controller and the Contact object is the model).

It looks like there’s two contenders in the .Net space (both ports of java) (there’s more here but they are fairly niche players).

Has anyone used either of these or other templatization engines? Are the key differences between them I should take into consideration when choosing

A: 

I just used RegEx.Replace and for each match I found between the { and } I looked up the tag in a Dictionary<,> and executed the ITagParser.

Dear {Salutation} {FamilyName}

The current date is {Now=yyyy-MM-dd}

Where the = is an optional format.

It was too little work to justify a 3rd party library.

Peter Morris
+2  A: 

I've used StringTemplate.NET and enjoyed the experience. It took a little while to adjust to their world view - things like looping seems cryptic at first. Not allowing templates to call into code kind of goes against the grain of most of the other templating libraries around. But once you get used to it, their decisions make sense and feel natural.