Writing.
Directives
Text template directives provide general instructions to the text templating engine about how to generate the transformation code and the output file.
For example, the following directive specifies that the output file should have a .txt extension:
<#@ output extension=".txt" #>
For more information about directives, see T4 Text Template Directives.
Text blocks
A text block inserts text directly into the output file. There is no special formatting for text blocks. For example, the following text template will produce a text file that contains the word "Hello":
<#@ output extension=".txt" #> Hello
Control blocks
Control blocks are sections of program code that are used to transform the templates. The default language is C#, but to use Visual Basic, you can write this directive at the beginning of the file:
<#@ template language="VB" #>!":
<# for(int i = 0; i < 4; i++) { Write(i + ", "); } Write("4"); #> Hello!
Instead of using explicit
Write() statements, you can interleave text and code. The following example prints "Hello!" four times:
<# for(int i = 0; i < 4; i++) { #> Hello! <# } #>
You can insert a text block wherever a
Write(); statement would be allowed in the code.
Note
When you embed a text block within a compound statement such as a loop or conditional, always use braces {...} to contain the text block.
Expression control blocks
An expression control block evaluates an expression and converts it to a string. This is inserted into the output file.
Expression control blocks are delimited by the symbols
<#= ... #>
For example, the following control block causes the output file to contain "5":
<#= 2 + 3 #>
Notice that the opening symbol has three characters "<#=".
The expression can include any variable that is in scope. For example, this block prints lines with numbers:
<#@ output extension=".txt" #> <# for(int i = 0; i < 4; i++) { #> This is hello number <#= i+1 #>: Hello! <# } #>:
<#@ output extension=".txt" #> Squares: <# for(int i = 0; i < 4; i++) { #> The square of <#= i #> is <#= Square(i+1) #>. <# } #> That is the end of the list. <#+ // Start of class feature block private int Square(int i) { return i*i; } #>:
List of Squares: <# for(int i = 0; i < 4; i++) { WriteSquareLine(i); } #> End of list. <#+ // Class feature block private void WriteSquareLine(int i) { #> The square of <#= i #> is <#= i*i #>. <#+ } #>
It is particularly useful to place a method that generates text in a separate file that can be included by more than one template.
Using external definitions
Assemblies
The code blocks of your template can use types that are defined the most frequently used .NET assemblies such as System.dll. In addition, you can reference other .NET assemblies or your own assemblies. You can provide a pathname, or the strong name of an assembly:
<#@ assembly name="System.Xml" #>
You should use absolute path names, or use standard macro names in the path name. For example:
<#@ assembly name="$(SolutionDir)library\MyAssembly.dll" #>:
<#@ import namespace="System.Xml" #>.
Transforming Data and:
<#@ import namespace="System.IO" #> <# string fileContent = File.ReadAllText(@"C:\myData.txt"); ....
Relative file paths in design-time templates
In a design-time text template, if you want to reference a file in a location relative to the text template, use
this.Host.ResolvePath(). You must also set
hostspecific="true" in the
template directive:
<#@ template hostspecific="true" language="C#" #> <#@ output extension=".txt" #> <#@ import namespace="System.IO" #> <# // Find a path within the same project as the text template: string myFile = File.ReadAllText(this.Host.ResolvePath("MyFile.txt")); #> Content of MyFile.txt is: <#= myFile #>.)
Editing Templates
Specialized text template editors can be downloaded from the Extension Manager Online Gallery. On the Tools menu, click Extension Manager. Click Online Gallery, and then use the search tool.