Using the Text Template Transformation Toolkit (T4) in Visual Studio

Test Template Transformation Toolkit (T4 templates) was introduced with Visual Studio 2005 as a separate installer; later it was provided out of the box with Visual Studio 2008 and 2010 installations. T4 templates can be used to generate source code or any kind of text content (.txt, .xml, .html, etc.) dynamically. Since it supports both C# and VB syntaxes the learning curve will be much less for developers.

Even Visual Studio itself is making use of the T4 templates in the case of MVC, Entity Framework and Linq To SQL for auto generating the source code. The extension of the T4 templates is .tt.

Control Blocks

Inside the T4 templates you can use different control blocks to perform various operations as mentioned below.

Directive Block

This block is used to specify the directives for the template. The block will start with <#@ and ends with #>. Some of the directives are template, output, parameter, etc.

<#@ template debug="false" hostspecific="false" language="C#" #>

Expression Block

This block is to provide an expression that will be converted to a string during the execution of a template. Below is a sample.

Today's date is <#= DateTime.Now #>

Statement Block

This block is used to generate the code on the output file. It starts with a <# and ends with #>. It can have code statements, declarations, etc., but cannot have method or class declarations. Below is a sample.

<#
for(int i=0; i<10; i++)
{
       WriteLine("Hello" + i);
}
#>

Class Feature Block

This block can be used to write things that should not be a part of the main transform. Helper methods, property declarations, etc. can be accommodated here. It should start with a <#+ and end with a #>. Below is a sample.

<#
for(int i=0; i<10; i++)
{
       WriteHelloText("Hello" + i);
}
#>
 
<#+
       private void WriteHelloText(string text)
       {
              WriteLine(text);
       }
#>

Executing a Template

When you add a template in Visual Studio it also adds a child file for the template that is based on the output extension that you specify on the template. The output is written onto the child file when the templates are executed. Templates can be executed in many ways.

1. Saving the template – When you make some changes and save the template, then the template is executed. The output can be witnessed on the output file once the template execution is successful.

2. Using Custom Tool – Right click on the template and select run Custom tool. This action results in the execution of the template.

3. In Visual Studio 2010 the solution explorer has a button, 'Transform All Templates'. Clicking on it will execute all of the template files available in that project.

4. Through code – Preprocessed templates will expose a method named TransformText, which will return the output of the executed template. It can be explicitly called from another class during runtime.

Different Types of T4 Templates

There are two types of T4 templates available with Visual Studio 2010. Based on their types they can either be executed during design time or during run time.

Text Template

This template is used during the design time.  It is mainly used to re-generate the source code based on needs like customization for a particular client or the source code needs to be updated based on some parameters from an external source like a database or XML. It uses the TextTemplatingFileGenerator custom tool.

Preprocessed Text Template

This is a run time template i.e. the execution of the preprocessed template would be triggered by another object during runtime. When you create a preprocessed text template in Visual Studio the output .cs file will have a default virtual method named TransformText and it can be called in other parts of the application. The TransformText method will return a string containing the output text. These templates can be used in scenarios like generating email content dynamically based on some given parameters. It uses the TextTemplatingFilePreprocessor custom tool.

T4 is a nice feature and it definitely becomes handy for developers. Only caveat that I see is the lack of intellisense and syntax help on the templates.

Happy reading!



Related Articles

Comments

  • good

    Posted by virus on 11/01/2012 04:05am

    good article man.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds