Many of you know me from some open source projects that are hosted on CodePlex:
The next project has been in development for quite a while and is closely related to the other projects, as it is being used in ProMesh.NET and it is based on the LazyParser.NET expression parsing engine.
So I'd like to introduce SharpTemplate.NET, a lightweight general-purpose template engine for .NET 2.0. It allows you to incorporate template parsing in your .NET applications. Perfect for code generators, reporting tools, mass-mailing applications, etc..
The syntax of SharpTemplate.NET is not fixed, unlike other engines (NVelocity, StringTemplate, ...). The syntax is fully configurable with just a few lines of code and some regular expression magic. Currently, the following syntaxes are built in:
To give you an idea of how it works, here is an sample of a template (using Velocity syntax):
<table>#foreach (product in Products) <tr><td>$product.Name</td> #if (product.Stock > 0) <td>In stock</td> #else <td>Backordered</td> #end </tr> #end </table>
The same template in "ProMesh.NET" syntax:
<table> <!--$[foreach product in Products]--> <tr><td>$[product.Name]</td> <!--$[if product.Stock > 0]--> <td>In stock</td> <!--$[else]--> <td>Backordered</td> <!--$[endif]--> </tr> <!--$[endfor]--> </table>
The code to parse and render the template:
SharpTemplate template = new SharpTemplate<Velocity>(); ParserContext data = new CSharpContext(); data["Products"] = GetProducts(); string renderedFile='template.RenderFile("template.htm", data);
Well, you get the idea. If you want to play with it, the code can be downloaded on CodePlex