BT

Text Template Transformation Toolkit in Visual Studio 2008

| by Abel Avram Follow 12 Followers on Apr 17, 2008. Estimated reading time: 1 minute |

Visual Studio 2008 includes a Text Template Transformation Toolkit, also known as T4, as part of Domain-Specific Language Tools, and can be used to generate code based on a text template. T4 can be also used with Visual Studio 2005 being included in the DSL and GAT toolkits.

According to MSDN:

A text template is a file that contains a mixture of text blocks and control logic. When you transform a text template, the control logic combines the text blocks with the data in a model to produce an output file. You can use text templates to create text artifacts such as code files and HTML reports. For example, a model of the flow of control between the individual pages in a user interface, such as a wizard, can be passed as input to a text template. The text template can generate code and configuration files that implement that flow of control.

A text template undergoes a transformation process which takes the text template file as input, transforms it, and generates an output text file. The transformation process has two steps. In the first step, the T4 engine uses the text template to generate a temporary transformation class. In the second step, the T4 engine compiles the transformation class into an assembly and executes the code, generating an output text file.

The "Hello World!" example looks like this:

<#@ template language="C#" #>
<# Write("Hello World!") #>

The text template can contain Visual Basic or C# code, C# being the default language if one is not specified. The output directive is used to specify the generated text file type. There can be many output file types, some of the most usual ones being .html, .txt, .cs, .vb. The default output file type is .cs. Following is an example on how to use the output directive:

<#@ output extension=".html" #>

There is limited support in Visual Studio for editing text templates. Clarius Consulting has a free editor "offering support for colorization of the different T4 template parts, IntelliSense, and design-time preview of templates". Visual Studio supports debugging of text templates and of the transformation process, but it is not straightforward. Detailed explanations on debugging can be found on MSDN, while Gareth Jones has a short tutorial. Oleg Sych has posted an introduction article on T4. The more comprehensive MSDN documentation is available here.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Example of Text Template Transformations in action ... by Graham Sibley

My company recently released a new product that leverages Text Template Transformations as part of a business-driven modeling and code generation platform that integrates with Visual Studio .Net. The product is called Viewpoints and you can check it (along with some T4 samples) out at:
www.gnomzsoftware.com/viewpoints/.

Re: Example of Text Template Transformations in action ... by Francois Ward

That looks pretty nice... That entity designer is quite pretty too. You guys really had fun with the DSL extension toolkit :)

Re: Example of Text Template Transformations in action ... by Billy McCafferty

If you'd like some real world T4 taken just about as far as it'll go, I'd invite you to take a look at the Crud scaffolding capabilities found within S#arp Architecture (code.google.com/p/sharp-architecture/). There's plenty of direction in the documentation found within the beta release.

Billy McCafferty

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

3 Discuss
BT