T4 templates and include files
February 15, 2009 Leave a Comment
It true that Microsoft provides no support for .tt template editing in Visual Studio 2008. They don’t plan to include this functionality in Visual Studio 2010 either – see feature request in Microsoft Connect. If Clarius Consulting hadn’t released their T4 Editor, there would be no support for editing T4 templates at all.
I have spend many hours lately in order to master T4 template creation. The most annoying thing about T4 templates, is that they work: TextTemplatingFileGenerator tool, the tool responsible transforming T4 templates to text artifacts, has no clue if a .tt file is a standalone template or an include template file. The tool transforms every file, without exception. When creating multi-file templates, this is really annoying – you get thousands of errors, which don’t really exist. When you are in the middle of template creation, this is really confusing.
How can you deal with this?
Actually there is only one solution: use .tt extension only for stand alone templates, or the starting template of multi-file template. Use an other extension, let’s say .ttinclude extension, for all other template files. TextTemplatingFileGenerator tool is not assigned as “Custom Tool” in the new extension, so transformation by default is disabled.
This looks nice, but .ttinclude extension, is not recognized as a known extension by Visual Studio or Clarius T4 Editor. You will get rid of the errors that are really annoying, but you loose all this nice functionality that Clarius T4 Editor provides. The good news is that you can deal with this, adding just two entries in your registry:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT.ttinclude]
@="VisualStudio.TextTemplating.1.2"
[HKEY_LOCAL_MACHINESOFTWAREMicrosoftVisualStudio9.0LanguagesFile Extensions.ttinclude]
@="{4217b9d4-cd01-4b28-bc12-e289a19ecc6d}"
The first one, provides a type for .ttinclude extension, and a icon to recognize the files. The second one, makes sure that Clarius T4 editor, recognizes .ttinclude extension as a T4 template, and provides T4 template editing.
Happy transforming!