tangible T4 Editor Blog

News, Tip and Tricks from the tangible T4 Editor team.

  • Home
  • Blog
  • How to generate multiple output files from a single T4 Template

How to generate multiple output files from a single T4 Template

Usually it is sufficient when transforming a T4 Template “example.tt” results in a single output file “example.cs”. But there are situation in which one would like to have multiple different output files like:

  • Putting each generated data class into a single code file
  • Creating a server and a client component based on a common model
  • Distribute generated code to multiple projects within a solution

This blob post demonstrates how to use the tangible T4 Editor and free template from its sample gallery to create multiple output files. The first part shows how to make a T4 Template generate two different output files with different content. The second part shows how to create multiple output files in different projects within your solution. Finally a summary of all necessary steps is given.

 

Part 1: Generate multiple output files

Create your template

First create a T4 Template in your solution by selecting “Add” > “New Item” from the context menu of your project. Choose the item type “Blank T4 Template” from the “tangible T4 Editor” section and name it “MultipleOutputFiles.tt”.imageThis template will contain the transformation code as in any other T4 Template scenario.

 

Include reusable Template Manager from Template Gallery

In addition to this template you need to use an includable T4 Template that provides the code that allows to output code into multiple files. This reusable template is available for free through the tangible T4 Editor Template Gallery.

Add another template to your project and locate it next to the “MultipleOutputFiles.tt” but this time choose “Reusable T4 Template” in the Add New Item-Dialog. Name this file “TemplateFileManagerV2.1.ttinclude”.

Your Solution explorer should look similar to this now:

image

Open the .ttinclude file by double clicking it and delete ALL its contents. Go to the tangible T4 Editor Template Gallery by right clicking somewhere inside the Editor window and selecting “Template Gallery.”

image

If you are prompted whether to download all available online templates to your local computer, choose “yes”. Otherwise you won’t be able to insert templates into your projects. In the TreeView on the left navigate to “.ttinclude (Use as Include-File)” > “.cs –> .cs” and select the entry “TemplateFileManagerV2.1”.
The template code will be shown in the preview part of the gallery.

Insert the code of this template to your file by clicking the “Insert Code” Button at the bottom of the Gallery screen.

image

Begin of the Template and Referencing the .ttinclude

Now that you obtained the code you need to include that file into the main Template “MultipleOutputFiles.tt”. To show you the parts that are really necessary, clear all content of this file, too.

Instead add the following lines:

        <#@ template language=”C#” hostSpecific=”true” #>

        <#@ output extension=”.cs” #>

This states that you will create your T4 directives with C# and the main output file will be “MultipleOutputFiles.cs”.

The next line will make the Templating Engine include the TemplateFileManager so that you can use its Methods and Classes within your Template:

        <#@ include file=”TemplateFileManagerV2.1.ttinclude” #>

 

Using the TemplateFileManager

Having this file included you can create an instance of the TemplateFileManagerClass provided by the .ttinclude.

<#

var manager = TemplateFileManager.Create(this);

#>

 

Until now all output code you write goes to the default output file “MultipleOutputFiles.cs”. To Create another file call the StartNewFile() method on the TemplateFileManager object. After calling this method all output code will be rendered into a new file with the name passed as parameter. The extension of the new output file may differ from the extension of the default output file.

 

<#

var manager = TemplateFileManager.Create(this);

#>

    // this output code will be written to MultipleOutputFiles.cs

<#

manager.StartNewFile(“Outputfile2.txt”);

#>

    // this output code will be written to Outputfile2.txt

 

Write output code to multiple files

To make the File Manager write the files, call the Process() Method.

 

<#

manager.Process();

#>

 

Test the output to multiple files

To transform the T4 Template, right click the MultipleFileOutput.tt in the Solution Explorer and select “Run Custom Tool”.

image

Both files will be created under the node “MultipleFileOutput.tt” and contain the proper output code:

image

 

 

Part 2: Multiple output files in different projects

There are cases in which generating multiple files within the same directory does not suffice. For example if you want to create a WebService based on an object model and would like to generate the client part in a different assembly than the server part.

The TemplateFileManagerClass provides you with that functionality, too:

 

In order to create a new output file in another project of the solution currently opened in your Visual Studio, you need to pass more parameters to StartNewFile() Method.

 

<#

manager.StartNewFile(“Outputfile3.txt”,
                     “SomeOtherProject”
                     “SubFolder”);

#>

 

This call will place the new output file named “Outputfile3.txt” in the project named “SomeOtherProject” in your currently opened solution within the folder “SubFolder”.
Make sure that a project named “SomeOtherProject” and a folder named “SubFolder” exist, otherwise the code generation will fail.

Omitting the parameter specifying the subfolder to place the output file in will make the TemplateFileManager create the output file in the root directory of the project.

 

image

 

Summary

This article demonstrated how to use the tangible T4 Editor to generate output to multiple files. The different output files can be placed in different projects within the solution currently opened in Visual Studio.

In short these are the steps necessary to create multiple output files:

  • Create a new T4 template file (e.g. named “MultipleFileOutput.tt”)
  • Create a new reusable T4 template file (e.g. named “TemplateFileManagerV2.1.ttinclude")
  • Open the .ttinclude file and import the content of the sample “TemplateFileManagerV2.1” from the T4 Template Gallery
  • Reference the .ttinclude within your T4 template “MultipleFileOutput.tt”
  • Create an instance of the class TemplateFileManager provided by the .ttinclude
  • Each time you want to start a new output file call the StartNewFile() method of the TemplateFileManager
  • At the end of your T4 template call the Process() method of the TemplateFileManager to write the output code
  • Transform your T4 template as usual.

If you want to put output files inside different projects within your solution use the overloads of the StartNewFile() method. Make sure that the projects and folders where you want to place the output files exists before transforming the template.

Twitter Updates

Authorization on the site

For an authorized user opens up new possibilities

Login

Password