Fake binary trees
... rants, ramblings and occasional good idea ...

Using assembly resources as NVelocity templates

For those who don't already know, NVelocity is a .NET port of Jakarta Velocity template engine. Castle team took it over, due to the lack of releases, support and bug fixes on the original port, and I strongly suggest to use this improved version.

NVelocity is a fine tool, but it is not quite obvious how to set it up so that embedded assembly resources can be used as templates. This is done by setting a group of properties which control what implementation of ResourceLoader abstract class is used to load the templates. To use embedded resources, you need to register the NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader class as ResourceLoader, and specify the assembly which contains the templates as parameter "assembly.resource.loader.assembly". Here's how: 

private static void InitializeVelocity()
{
  ExtendedProperties properties = new ExtendedProperties();

  properties.AddProperty("resource.loader", "assembly");
  properties.AddProperty("assembly.resource.loader.class",
          "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity");
  properties.AddProperty("assembly.resource.loader.assembly",
          Assembly.GetExecutingAssembly().GetName().Name);
     
  m_velocity = new VelocityEngine(properties);
}

You can find the full sample here: NVelocitySample.zip (87.13 kb)

Posted at 18:34 on September 19, 2008
Categories: .NET | NVelocity   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed