Home > Articles > MVC > Using StringTemplate as a ViewEngine for ASP.Net MVC > Building a StringTemplate ViewEngine

Building a StringTemplate ViewEngine

My next step was to construct a view engine for string templates that plugs into the MVC framework. Following the lead of the MVC team, I extended the IViewEngine and IView interfaces and came up with a pretty simple one.

My requirements for a ViewEngine were as follows:

  1. Templates must be cached.
  2. Cached templates must be refreshed based on a File System change. Otherwise development is a real pain.
  3. For enhanced performance, templates must be rendered to the Output stream, and not generate a monstrous string that gets dumped to the Response.

Little did I know, those requirements would be easier to meet than I thought. After poking around in the StringTemplate documentation, I found out that templates are cached natively, and utilize a FileSystemWatcher to detect changes. In addition, the object that renders templates can dump to a string, as well as a Stream. If only all of my coding endeavors worked out this way.

I won’t dig too deep into the details, but the implementation was pretty straightforward. Rather than blog about it, I’ll just let the code speak for itself. I created a Google Code project to host my ViewEngine. Feel free to download and kick the tires.

StringTemplate View Engine source project @ Google Code.

< Prev Next >