# Tuesday, May 20, 2008

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:

  • SharpVelocity (Velocity-like syntax)
  • ProMeshHtml (syntax used by ProMesh.NET)
  • XML
  • "DoubleCurly"

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

kick it on DotNetKicks.com
Tuesday, May 20, 2008 10:26:17 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [4] -

Tracked by:
"Link Listing - May 20, 2008" (Christopher Steen) [Trackback]
Wednesday, May 28, 2008 1:36:08 PM (W. Europe Daylight Time, UTC+02:00)
Hi Philippe,

SharpTemplate looks really interesting. I have a need for a template engine, and had just decided that StringTemplate was the one for me. It seems to have so much history and momentum behind it. However, yours also looks interesting to me. Are there any in-depth comparisons of the two engines (i.e. what you lose/gain with one over the other)?

Cheers,

Ben
Ben Taylor
Tuesday, June 03, 2008 12:09:23 PM (W. Europe Daylight Time, UTC+02:00)
Hi Philippe

Thank you for the great library. I'd like to use the library to generate templates from dynamic data tables. What you think the best way would be to allow for this?

Kind Regards

Shaun Trennery
Wednesday, June 04, 2008 2:01:46 PM (W. Europe Daylight Time, UTC+02:00)
@Shaun: please visit the support forum at http://www.forum.activa.be . Please post your questions there. Maybe you can explain what you want to do, and I'll try to help.
Friday, June 20, 2008 6:54:17 PM (W. Europe Daylight Time, UTC+02:00)
Hi Philippe
Thanks for your great work!Can you write some artilces(Examples) about using SharpTemplate.Net? I am interested in your Template Library.Thanks!
goalbell
Comments are closed.