Peter works on the web!

Using spark as a html/text rendering engine

with 5 comments

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?

Written by Peter

October 30th, 2010 at 5:41 pm

Posted in Uncategorized

5 Responses to 'Using spark as a html/text rendering engine'

Subscribe to comments with RSS or TrackBack to 'Using spark as a html/text rendering engine'.

  1. What if my model is an anonymous type? Obviously, inheriting from AbstractSparkView isn’t an option if my model is anonymous. Thoughts?

  2. Hi Byron,

    I’m a bit behind on mails so forgive me for answering a bit late. I think what you could do is create an “Anonymous view” and have that one inherit from AbstractSparkView. Then you got to make an Date property of the object type. I’m guessing that Spark will then be able to access the properties the anonymous object has. I’d have to try it out though. This is all hypothetical.

    It would be something like this:

    public AnonymousView : AbstractSparkView
    {
    public object Data { get; set }
    }

    Let me know if it works, I’ll try it out myself when I have the time.

    Peter

    24 Apr 11 at 7:50 am

  3. [...] use Spark as engine to generate all sorts of HTML but on Monday last week that specific code snippet failed without being changed. Spark indicated [...]

  4. [...] couple of hours with the library and I really like it. On one of my previous posts Byron Sommardahl asked if it was possible to use an anonymous object as the model of your Spark view. I proposed a solution but I didn’t really have an idea if it actually worked. Because I was [...]

  5. Byron, I tried out the solution I proposed. I got it to work so if you’re interested you can find the solution on: http://peter.worksontheweb.net/2011/05/20/using-an-anonymous-object-as-the-model-for-your-spark-view/

    Peter

    20 May 11 at 9:42 pm

Leave a Reply