T4 – Template and TemplateTemplate

Update: use this tip instead.

One can call a template from another through <#@ include file=”MyTemplateTemplate.tt” #> Variables defined in MyTemplate.tt are then transfered to MyTemplateTemplate.tt. The trick is to set them in the right order.

Below is also shown a way to avoid unnecessary output. Here is [another].

MyTemplate.tt:

<#
string Greeting = "HelloWorld";
bool createOutput = true;
#>
<#@ include file="MyTemplateTemplate.tt" #>

MyTemplateTemplate.tt:

<#
if( !createOutput )
{
return "//	This file is not in use.";
}
#>

…the template code

<#+
bool createOutput = false;
string Greeting = string.Empty;
#>

As you can see the TemplateTemplate has the variable assignment as a statement “<#+” below/after the code.

Then there is the createOutput flag as a trick to avoid output. Return blank if you want an empty file. Another way to avoid compilable code that won’t compile is here.

Leave a Reply