What is the Visual Studio T4 text processor?
Posted: (EET/GMT+2)
Even if you have been using Visual Studio for a while, you might not have run into a Visual Studio feature called T4. T4, or TTTT, actually stands for Text Template Transformation Toolkit. But what is this toolkit, and what is it used for?
Let's take a concrete example from ASP.NET MVC. If you are creating a new view with the Add View command, Visual Studio creates a new .aspx page with ready-made HTML code. But where does this code come from? It comes from file-based templates.
Where do these templates live? By default, Visual Studio installation creates a folder called "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE", which contains numerous subfolders for different template types, including those in the New Project/New Item dialog boxes. For ASP.NET MVC templates, there's a folder called "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC\CodeTemplates\AddView". This folder in turn contains template files with the .tt extension.
These files contain simple scripts with the T4 is able to process, and then output text data based on the results of running the scripts, similar to processing the <% and %> tags in .aspx files.
Sounds interesting, right? The good thing is that you can even utilize this engine in your own projects. For more details, see the MSDN documentation.