An open source template engine for C#

 

Purpose

"TemplateMaschine" is a free open source tool which processes templates written in C# and outputs the results to either a file or a string. Other software of this category include CodeSmith or the Java-based Velocity template engine.

It's a tool perfectly suitable for MDSD (Model Driven Software Development) resp. MDA (Model Driven Architecture) and can be used for a variety of applications of code generation including

  • generating code from UML diagrams (e.g. from XMI files)
  • generating stored procedures
  • generate documentation from classes
  • ...and much more

Short-spoken, it can be used to relieve you from all kind of boring and error prone "cut-and-paste" programming.

 
Features
  • templates use ASP.NET-like syntax (see below for an example)
  • you write your templates in C#, which can also reference other assemblies and import namespaces
  • templates can be called with arguments
  • templates can be loaded from a file or embedded as a resource in an assembly, which is useful if you want to write 'packaged' generators
  • templates can include other templates
  • script blocks with custom functions and variables can be added to a template
  • the template processor is command line based, that's why it's easy to include template processing in build-scripts like NAnt
  • it includes error handling, which means that line number and error information are provided in case of syntax errors in your templates (does not yet work well in all cases though, but I think it's nevertheless very useful)
  • intermediate generated template code can be written to a file for debugging purposes
  • it's free and open source (released under GNU Lesser General Public License)
  • code is written in C#
  • it's fast
 
Introductory Example

We start with a simple example, which consists of the following files:

  • This is the driver application. It loads the template, and calls the generate() method that processes the template. Output is written to 'SampleOutput.txt'
  • This is a simple template without arguments
  • Compiles the application and executes the sample

So, here's the driver code:

File SampleDriver.cs:
using System;
using TemplateMaschine;

namespace Sample
{
  class Program
  {
    static void Main(string[] args)
    {
      // Load a template from file 'Sample.template' 
      // and compile it
      Template myTemplate = new Template("Sample.template");

      // Process the template with no arguments
      // and write output to file 'SampleOutput.txt'
      myTemplate.Generate(null, "SampleOutput.txt");
    }
  }
}
Go to the [download section] to download the complete source code.

Here comes the template:

File Sample.template:
This is the first line
<% string[] greetz = new string[] {"Hello", "this", "is", 
                                "my", "first", "template"};
   foreach(string greet in greetz)
   { %>
Here comes the text: <%=greet%>
<% } %>
This is the last line
Go to the [download section] to download the complete source code.

Now, call run.bat which compiles the files and executes the sample driver using the following commands:

C:\> csc /t:library TemplateMaschine.cs
C:\> csc /t:exe /r:TemplateMaschine.dll SampleDriver.cs
C:\> sampledriver.exe

Now have a look at 'SampleOutput.txt', which should have appeared in your directory:

File 'SampleOutput.txt':
This is the first line
Here comes the text: Hello
Here comes the text: this
Here comes the text: is
Here comes the text: my
Here comes the text: first
Here comes the text: template
This is the last line
Go to the [download section] to download the complete source code.
 
More Examples

Here are some more examples that show enhanced features (for a complete list of features, see the documentation section):

 
 

Requirements:

Sources can be found at sourceforge.net project page

  SourceForge.net Logo  

 
Feedback

Please send me your

If you like the software I would be very pleased if you donate me an amount of your choice to support my work. Thank you very much in advance!