SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

T4 Templates in SharpDevelop

SharpDevelop 4.1 now has integrated support for T4 Templates. The T4 code generation engine for MonoDevelop has been used to make this feature possible so a big thanks to Michael Hutchinson for creating this. Features include:

  • Syntax highlighting for T4 templates (.tt and .t4 files).
  • Support for TextTemplatingFileGenerator and TextTemplatingFilePreprocessor custom tools.
  • T4 template errors shown in Errors window.

Code completion for T4 templates is not currently supported.

Creating a T4 Template

Open a project in SharpDevelop, right click the project and select Add, New Item. This will open the New File dialog. There are two T4 templates available, one for C# and one for VB.NET.

C# T4 template selected in New File dialog

Select the appropriate file template, give it a name and click the Create button.

New C# T4 template open in text editor.

Now you can edit the T4 template. In the example below we have modified the T4 template so it generates the current date and time when it is saved.

<#@ template language="C#" #> 
<#@ output extension=".txt" #> 
 
Date and time: <#= DateTime.Now #>

When the file is saved a new file will be created. In the Projects window this new file will be a child of the T4 template file.

File generated from T4 template in Projects window.

Opening this file in the text editor shows text generated by the T4 template.

File generated from T4 template open in text editor.

Creating a Preprocessed T4 Template

SharpDevelop also supports preprocessed T4 templates. A preprocessed T4 template will create a class which can be used by your application at runtime to generate text. We will take our previous template and convert it to a preprocessed T4 template. To do this select the T4 template file in the Projects window, right click and select Properties. Select the Custom Tool property and change it to be TextTemplatingFilePreprocessor.

T4 template custom tool property changed to TextTemplatingFilePreprocessor

Now when you save the T4 template a file will be generated containing a class.

Preprocessed T4 template generated file.

This class can then be used in your application to generate text at runtime. Shown below is a simple example of how this class could be used.

using System; 
 
namespace MyProject 
{ 
 class Program 
 { 
  public static void Main(string[] args) 
  { 
   MyTextTemplate template = new MyTextTemplate(); 
   string text = template.TransformText(); 
    
   Console.WriteLine(text); 
   Console.ReadLine(); 
  } 
 } 
}

The result of running the above code is shown below.

Preprocessed T4 template class output displayed in Console output.

For more information on creating T4 templates take a look at Oleg Sych's excellent set of T4 tutorials.

Published Jun 26 2011, 06:38 PM by MattWard
Filed under:

Comments

 

Michael Seeger said:

Thank you so much for this feature! It was one of the ones I hoped to be implemented, soon. And now it's done. Great Job!

June 27, 2011 9:28 AM
 

robbine said:

How can i join this great project?

Is there some way to join it like use svn?

June 29, 2011 11:25 AM
 

MarcusH said:

The SharpDevelop wiki have information about how to join the team

wiki.sharpdevelop.net/JoiningTheTeam.ashx

The source code is hosted on github

github.com/.../SharpDevelop

June 29, 2011 1:21 PM
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.