IT:AD:Transformations:T4:Syntax:Directives
Directives
Just like in ASP.NET, the page starts by defining directives that control the outcome of the transformation.
These directives are the following:
- template
- assembly
- include
- import
- output
- parameter
Directives are a combination of <# and @.
Template
Defines page as a template:
<#@ template language="[C#|VB]" [hostspecific="True"][debug="True"]#>
Elements:
@language: self evident@hostspecific="True": in Visual Studiotruegives access toGetServiceto update your text templates.- Ref: (Oleg Sych)(http://www.olegsych.com/2008/02/t4-template-directive/)
@debug="True": self evident
Include
Includes file as if it was part of the current *.tt file.
<#@ include file="filePath" #>
Elements:
@file: name of file to include
Examples:
<#@ include file=”T4Toolbox.tt” #> <#@ include file="EF.Utility.CS.ttinclude"#> //cf: POCO template
Import
Equivalent of using in C#
<#@ import namespace="System.Diagnostics" #>
Output
use to specify characteristics of the generated text output transformation of input
<#@ output extension=".cs" [encoding="utf-8"] #>
Elements:
@extension: extension of output file generated by transforming the input (the .tt)@encoding: default is same as always (utf-8)
Assembly
Loads an assembly so that your template code can use its types. The effect is similar to adding an assembly reference in a Visual Studio project.
<#@ assembly name="[assembly strong name|assembly file name]" #> //Referencing the output from another solution <#@ assembly name="$(SolutionDir)\MyProject\bin\Debug\MyProject.Dll" #>
Elements:
@name: generally a path- Documentation: MSDN
Parameter
declares a property in the current content, initialized from values passed in from the external context. Values can be passed either in the Session dictionary, or in CallContext.
<#@ parameter name="PropertyName" type="FQTN" #>
Elements:
@name: the name you want to use@type: the fully qualified type name (e.g.: System.Int32)Documentation: MSDN
Example of later references:
<# for (int i = 0; i < TimesToRepeat; i++) { #>
Line <#= i #>: Blah...
<# } #>