Using spark as a html/text rendering engine

30/10/2010

I can't remember a project which I worked on that didn't have a requirement which included sending out emails. Most of the time the templates that are needed for these emails are so small that a simple text file with some wildcards does the trick. It does the trick but nothing more. It's hardly an elegant solution and there is no support to loop through elements or do any other complicated stuff. I wanted a better, broader solution so I started to look around for usable libraries. I have been hearing a lot of good things of the Spark View Engine and wondered if it was possible to use it to generate html from a given template.

Most articles I found were about using Spark with ASP.Net MVC, only one talked about using Spark as a general purpose template engine by Louis DeJardin. Although dating from 2008 it is still very relevant. The article uses an in-memory template so I changed it that it reads its templates from a folder on the file system. This is certainly no rocket science and most of the code is based on the code of Louis but here goes:

First thing to do is create a Spark template, we'll be using this very simple example:

<div>
    <strong>${name}</strong>
    <span class="address">${address}</span>
</div>

Now we need to create a corresponding abstract view with the properties that will be mapped to the template:

public abstract class ASimpleView : AbstractSparkView
{
    public string name { get; set; }
    public string address { get; set; }
}

There are 2 things you need to remember when creating a view code file. The view must inherit from the AbstractSparkView class and the names of the properties in the view have to be the same as the names in the template file. So, obviously, the property 'name' in the view maps to ${name} in the template file.

And at last we glue it all together:

var settings = new SparkSettings().SetPageBaseType(typeof(ASimpleView));

var templates = new Spark.FileSystem.FileSystemViewFolder(HttpContext.Current.Server.MapPath("~/Templates"));
var engine = new SparkViewEngine(settings)
             {
                ViewFolder = templates
             };

var descriptor = new SparkViewDescriptor();
descriptor.AddTemplate("simple\_template.sprk.htm");

var view = (ASimpleView)engine.CreateInstance(descriptor);
view.name = "Name";
view.address = "Address";

var stringWriter = new StringWriter();
view.RenderView(stringWriter);

var html = stringWriter.ToString();

Let's go briefly through this snippet. We first create a SparkSettings object on which the basetype is set to our view. We load the templates via the filesystem, in this example the templates are located in the /Templates folder. Then we instantiate the spark view engine and a descriptor for our specific template. As you can see, after creating an instance of our specific template, the properties defined in the template are strongly typed and we can fill them out with the correct data. Finally we render the view into a StringWriter, getting the html from the StringWriter is as easy as calling the ToString method.

With this solution we can leverage Spark in our templates. I think this is a pretty elegant solution. Any opinions on this matter? What do you use to generate html in your projects?