Dependent T4 Templates – Automatically transform a T4 Template every time an input file is saved to generate new code
Date: Friday, February 22, 2013 4:00 PM
Posted by: Nico Ploner
This example shows how to attach a T4 Template file to a given input file so that the template is transformed each time the input file is saved.
In most scenarios T4 template are used to transform the contents of a given file into a different format (C# code, XML, …). That way the T4 template file becomes dependent on the input file. The usual workflow then is: edit the input file, save the input file and transform the template again to make sure that all changes are transformed again.
From time to time one might simply forget to transform the template after editing the input file. That’s when it would be great to have the edit-save-generate procedure in one simple step. This is where the “Dependent Templates” feature of the tangible T4 Editor comes into play.
In the Solution Explorer right click the file you want to be the input file for a dependent T4 template and choose “Add T4 Code Generator”.
The tangible T4 Editor then will add a new T4 template attached to the previously selected input file. This template contains some sample code about how to access the input file and generate output code from it.
The attached T4 template is transformed every time you edit and save the input file.
The “Dependent Templates” feature is only available in the Pro-Version of tangible T4 Editor. You can buy the Pro-Version here.