Scott Watermasysk

Still Learning to Code

SimpleTemplate

By now it should be obvious that I am big fan of the open source component NVelocity. Most of the examples of its usage are UI related such as view engines for MonoRail and ASP.Net MVC. We (Telligent) also use it to power the theme engine for Graffiti. While it is great to be able to use it in this way, the are a lot more non-web UI uses for it.

The other day, I received a comment asking for an example of how to use NVelocity, so I decided to put together a quick sample component, SimpleTemplate.

SimpleTemplate has one goal, take a string that represents an NVelocity template along with the data you want to insert/replace in that template and return the results. If you find yourself using constantly using string.Format or StringBuilder.AppendFormat to build complicated strings, SimpleTemplate should be able to make things much easier for you.

Here are a couple of quick examples of what you can expect it to do:


[Test]
public void SingleItemTest()
{
    const string result = "Hello World";
    const string template = "Hello $test";
    var iContext = TemplateEngine.CreateContext();
    iContext.Put("test", "World");
    Assert.IsTrue(result == TemplateEngine.Parse(template,iContext));
}

With a custom object:


[Test]
public void ObjectWithPropertyTest()
{
    const string result = "Hello World";
    const string template = "Hello $to.Test";
    TestObject to = new TestObject() {Test = "World"};
    var iContext = TemplateEngine.CreateContext();
    iContext.Put("to", to);
    Assert.IsTrue(result == TemplateEngine.Parse(template, iContext));
}

Finally, just to show off, anonymous types:


[Test]
public void TestAnonymousTypes()
{
    var anonymousType = new {Test = "Hello", Child = new {ChildTest = "World"}};
    const string result = "Hello World";
    const string template = "$at.Test $at.Child.ChildTest";
    
    var iContext = TemplateEngine.CreateContext();
    iContext.Put("at", anonymousType);
    Assert.IsTrue(result == TemplateEngine.Parse(template, iContext));
    
}

If you remove null argument handling code, the TemplateEngine class is probably about 15 lines of code. I have included the full source in the download.

NOTE:

I was browsing my local CastleProject source folder to see if there was a license I should be including with the NVelocity assembly when I noticed a project called TemplateEngine There is not much information on the site about it and I only briefly browsed the included test suite, but it does look very nice. SimpleTemplate will certainly get you started, but I would recommend checking out TemplateEngine as well. I will do a follow later after I have had some time to play with it.

Download SimpleTemplate.