Microsoft has a template engine bundled in with its DSL tools and with GAT (Guidance Automation Toolkit). Unfortunately it is a small pain to find out how to use it in your own code. Learning to write templates isn’t a big deal – there are tons of samples included with GAT.
The good news here is once you figure it out this thing is a piece of cake. Basically the secret is that you have to reference two things:
· Microsoft.VisualStudio.TextTemplating
· Microsoft.Practices.RecipeFramework.VisualStudio.Library
The first assembly is the T4 engine itself. But in order for the engine to run it needs a template host. Unless you want to write your own (not necessarily a bad idea) you will need to provide one. As it happens GAT provides you one free of charge.
So here is what the code looks like:
The basic outline of this code comes from – “Testing T4 templates using the GAX host” written by Jose Esrich. Now to put T4 to work for my own stuff.
Oh – you can grab my trashcode sample here. It’s hardly rocket science but no reason to retype it.