How to call GAX-based T4 Templates (that have a Property Directive) from standard T4 Templates

The property directive is one of the most discussed features of T4. By Microsoft it is implemented in the GAX Host not in the standard Visual Studio Host. This means that when you run a template by saving it with the standard mechanism (DSLTools) in Visual Studio it will fail.

Although there are other good mechanism for making reusable templates users of GAX/GAT and Blueprints need to rely on this mechanism.

But how can can  work with GAX  templates and call them from standard Text Templates, then?

1. You need to install GAX from here.

2. You need to get the T4GAXRunner.ttinclude template

Luckily tangible published a TT-Include File that provides the necessary code. You find it in the in-product template gallery of tangible’s T4 Editor. You can read about the gallery here.

The Code to use T4GAXRunner looks like this. The anonymous object encapsulates the parameters and values you want to pass as members.

<#@ include file="T4GAXRunner.ttinclude" #>
<#   GetT4GAXRunner().Render("GAXSample.t4",
        new {ClassName="MyClass", Namespace="ns"}); #> <

This would then call a typical GAX.tt file like this one.

<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ property processor="PropertyProcessor" name="ClassName" type="System.String" #>
<#@ property processor="PropertyProcessor" name="Namespace" type="System.String" #>
namespace <#= Namespace #>
{
    class <#= ClassName #> {
     // more…
   }
}

Conclusion

You can run your GAX based files from a standard T4 files. Just download tangible’s T4 Editor and get the template from the gallery.

No comments: