IT:AD:Transformations:T4:Syntax: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 @.

Defines page as a template:

<#@ template language="[C#|VB]" [hostspecific="True"][debug="True"]#>

Elements:

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

Equivalent of using in C#

 <#@ import namespace="System.Diagnostics" #>

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)

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

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...
 <# } #>

 
  • /home/skysigal/public_html/data/pages/it/ad/t4/syntax/directives.txt
  • Last modified: 2023/11/04 02:00
  • by 127.0.0.1