SimpleTemplate

written by Scott Watermasysk on Thursday, June 05 2008

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.

Similar Posts

  1. Setting Up Velocity (Distributed Cache)
  2. MvcContrib NVelocity Extension
  3. Yahoo! Search BOSS C# Implementation
  • SimpleTemplate on on 6.06.2008 at 8:44 AM

    You've been kicked (a good thing) - Trackback from DotNetKicks.com

Comments

  • Steve on on 6.06.2008 at 12:08 AM

    Steve avatar

    You post has me excited about NVelocity. I was looking at the CastleProject site for license terms as well. I assume the ASL in the full CastleProject download applies to all component, including the forked NVelocity.

    The open source licensing are new to me and as I am looking at potential usage in my company's closed source software I am scratching my head on the leagalese.

    Knowing that you are using it in Grafitti (which is not open source I believe), are there any license gotchas to using it (assuming just referencing the DLL and not altering any source)?

    I know, I know, legal disclaimers...not a lawyer, etc apply, just curious if you could offer some insight.

    Thanks!

Comments are closed