This project is read-only.

T4 Text Template Directives

A directive determines how the template will be processed.

Template Directive

The template directive contains all processing information needed to parse and compile the T4 text template. The should be only one template directive in a template. All further template directives, e.g. in included templates, will be ignored.

<#@ template [language="C#"] [compilerVersion="version"] [classname="class"] [codefile="file"] #>

language attribute

The language attribute defines the programming language used in the control blocks and in the code file. OMS.Ice supports C# and VB. This attribute is optional. The default language is C#.

compilerversion attribute

The compilerversion attribute specifies the .NET compiler version to use when compiling the generator. Possible values are v3.5 or v4.0. This attribute is optional. If not set, the .NET runtime will choose the appropriate compiler version. To take the version specified in this attribute, you need to specify CompilerSettings.Template in the generator options. The compiler version can be overwritten in the generator settings.

The compilerversion attribute is an extension to the Visual Studio T4 text template language.

classname attribute

This attribute can be used to provide a full qualified class name (namespace and class name) for the generated text generator class. This attribute is optional. The default value is OMS.Ice.T4Generator. + the name of the template file without the extension. The generated text generator class is marked as partial and can be extended by our own partial class.

The classname attribute is an extension to the Visual Studio T4 text template language.

codefile attribute

The codefile attribute defines the subsidiary code file containing a partial class to extend the generated text generator. The full qualified class name must match the class name given in the classname attribute. 

The codefile attribute is an extension to the Visual Studio T4 text template language.

Assembly Directive

Assembly directives have the same meaning as the assembly reference in a Visual Studio project. You must add the appropriate assembly directives for all types you use inside your T4 text template.

<#@ assembly name="[assembly strong name|assembly file name]" #>

name attribute

The name can be either the strong name of an assembly in the GAC or the absolute path to the assembly. The assemblies must be available at runtime.

Import Directive

The import directive is the equivalent to the using statement in C# or the imports statement in VB.

<#@ import namespace="namespace" #>

namespace attribute

The namespace attribute defines the namespace to import, e.g. System.Collections.Generic.

Parameter Directive

You can use the parameter directive to declare a property that can be used in the template. The values are passed through . You must use one parameter directive for each property. The parameters must be passed to the Generate or GenerateToFile method of the template generator in the same sequence as they have been declared in the template.

<#@ parameter type="Full.TypeName" name="ParameterName" #>

type attribute

The fully-qualified type name. If the namespace is referred by an import directive, the type name only would be sufficient. You can also use the build-in types such as int  (C#) or Integer (VB).

name attribute

The name of the property. This can be any valid identifier.

Include Directive

A T4 text template can include other templates. The OMS.Ice – T4 Text Template Generator supports two different include modes. 

Any template directive of an included template will be ignored. Assembly and import directives in included files will be treated as they were in the including template file. The usage of the include and parameter directives depends on the mode parameter.

<#@ include file="file" mode="mode" #>

file attribute

The template file to include.

mode attribute

The mode attribute can be either inline or method. Default value is inline.

If you choose the inline mode, the include directive will be replaced by the included template without any directives. The included text template will be treated as if it were part of the including template.
if the included template contains parameter directives, these parameters must be provided together with the parameters of the including template as described in the Parameter Directive section.
The inline mode does not allow including templates recursively. That means, an included template must not include the including template.

If you select the method mode, the included template will be used as a method and the include directive will be replaced by a method call. All parameters defined in the included file will be used as method parameters. These parameters can be provided by using the same parameter directives in the including file. Instead of using parameter directives, you can also declare the parameters as variables inside a standard control block of the including template. In this case, the variables must be declared before the include directive.
The method mode allows including templates recursively. However, you will need a break condition in at least one control block to not end up in an endless loop.

The mode attribute is an extension to the Visual Studio T4 text template language.

Comment Directive

The comment directive can be used to add comments to a template. Comments will be ignored when compiling the text template.

<#—- comment #>

The comment directive is an extension to the Visual Studio T4 text template language.

Last edited Jun 5, 2012 at 4:31 PM by olafmeyer, version 39


No comments yet.