Share T4 template files between TDS projects

July 07, 2016 | Angel Uzunov

The template based text generation framework T4 (Text Template Transformation Toolkit) included with Visual Studio is a general-purpose templating engine used to generate Visual Basic code, XML, C# code, HTML, or text of essentially any kind. Template files are created, a mix of literal text, code, and processing directives after which the T4 template processor will transform the file into a text file of the desired type by executing the code and processing directives inside.

Code Gen (added in TDS 4.0) allows developers to save time by generating Models (code classes) based on their Sitecore templates. This is much better than spending vast amounts of time writing the classes manually, repeating similarly structured code over and over again. Indirectly means that removal of fields or templates will produce build errors, alerting developers to issues at compile time rather than at runtime.

With TDS 5.5, T4 templates can now be added to a project as a linked file instead of a file. This allows for code gen across multiple projects to be performed by the same T4 files. When users are working on large solutions that have many projects, adding a T4 template to a project as a linked file prevents them from having to duplicate and maintain T4 templates for every single project. They can instead reference a single set of T4 templates across all projects. This reduces duplication and therefore standardizes code gen across the entire solution; the maintenance on T4 template files becomes simpler and easier.

This works exactly the same as other linked file types in Visual Studio (Share code with Add as Link). 
The steps to add a linked template are the following:
  1. Right-click on the Code Generation Templates folder in the solution explorer.
  2. Choose “Add Existing Item”
  3. Browse to the files in another TDS project
  4. On the add dialog, there is a little down arrow next to the “Add” button. Click that and select “Add as link”