Template Language

Templates are created and edited with a convenient editor, which provides context-sensitive code completion, error check, syntax highlighting and other features to speed up your work. All error and warning messages reported for errors in syntax of control code (not template compile-time errors) are automatically displayed in the Error List window. The Error List displays an error or warning message and the error location. You can easily navigate to the line that caused the error by double-clicking the error in the Error List window. Thus you may find and fix template errors quickly and conveniently.

Images_T4-editor

Template language is T4-like and can be used to generate custom output from the Entity Developer model.

 

ExpandedToggleIcon        Text Blocks

A text block is any non-programmatic text in a template. It can be added to a template by simply typing the text in it. Text blocks are passed to the output without changes.

 

ExpandedToggleIcon        Statement Blocks

Statement blocks are used to control the flow of processing in the text template. A statement block must be surrounded with '<#' and '#>' template tags (without quotes). In the statement blocks, you may use C# or Visual Basic statements to output things conditionally or to iterate over data to output things repeatedly for each item in that data. Statement language is determined by the template directive. Here is an example of the template with the template directive, statement blocks and a text block.

<#@ template language="C#" #>
<#
    for(int i = 1; i <= 3; i++)
    {
#>
Some text.
<#
    }
#>

The template output will be the following:

Some text.
Some text.
Some text.

 

ExpandedToggleIcon        Expression Blocks

Expression blocks can be used to add an expression value, converted to a string, to the generated code. Usually expression blocks are used inside text blocks to place calculated values from external data (for example, model data) to the generated code. The expression in the expression block is calculated, converted to a string, and then placed to output.

Expression blocks must be surrounded with '<#=' and '#>' template tags (without quotes). Here is an example of using an expression block.

<#@ template language="C#" #>
<#
    for(int i = 1; i <= 3; i++)
    {
#>
Some text <#= i #>.
<#
    }
#>

The template output will be the following:

Some text 1.
Some text 2.
Some text 3.

 

ExpandedToggleIcon        Class Feature Blocks

Class feature blocks allow adding custom functions and fields to avoid repeating common code. You may call these functions inside the statement blocks and use fields in the expression blocks.

A class feature block must be surrounded with '<#+' and '#>' template tags (without quotes). You may use expression and text blocks with the class feature block functions. Here is an example of using a class feature block.

<#@ template language="C#"#>
<# WriteSomeText(); #>
<#+
    private void WriteSomeText()
    {
#>
Some text.
<#+
    }
#>

The template output will be the following:

Some text.

 

ExpandedToggleIcon        Template Directives

Template directives are used to specify the language, whose syntax will be used when processing the statement, expression, and class feature blocks. Allowed values for the language parameter are "C#" or "VB". A template directive starts with the '<#@' directive opening tag, which is followed by the 'template' keyword, and the directive is closed with the '#>' closing tag. Then the language parameter is set. See the previous examples to learn how to use template directives.

 

ExpandedToggleIcon        Assembly Directives

Assembly directives are used to reference external assemblies, in order to use types within those assemblies from code in the text template. They have the name parameter, which must be set to a rooted file path or an assembly name. Here is an example of an assembly directive.

<#@ assembly name="System.Data" #>

 

ExpandedToggleIcon        Import Directives

Import directives enable the use of specified namespace identifiers without providing a fully qualified name in a text template. They have the namespace parameter which must be set to the namespace for import. Here is an example of an assembly directive.

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

 

ExpandedToggleIcon        Include Directives

Include directives are used for including templates, i.e. when in your template you want to use functions implemented in another template. So you can create a template file, implement a frequently used function in it, and by means of referring to this file from other templates, use this function.

<#@ include file="Validation.tmpl" #>

 

ExpandedToggleIcon        Property Directives

Property directives are used to declare properties of the template. Here is an example of a property directive.

<#@ property name="Output" category="Output" type="OutputInfo" editor="OutputInfoEditor" 
description="Specifies output for the generated entity classes." #>

The property directive can have the following parameters:

name - this is the name of the property that is displayed in the Properties area and can be used in template code;
category - this is the name of the category the property is included in;
type - this is the .NET type of the property values;
default - this is the value of the property by default;
editor - this is the editor class to be used for editing the property;
converter - this is the type to be used as a converter for the object this attribute is assigned to;
description - this is the description of the property.
displayName - the name, that will be displayed in the Properties window.

 

ExpandedToggleIcon        Extended Property Directives

Extended property directives are used to declare extended properties of model objects. Here is an example of an extended property directive.

<#@ extended name="GenerateDataContractAttribute" owner="Class" type="System.Boolean" default="True" 
description="Determines whether the DataContract attribute used for the serialization of entities should be generated." #>

The extended property directive can have the following parameters:

name - this is the name of the extended property that is displayed in the Properties area and can be used in template code;
owner - this is the model object type, which the extended property is assigned to. This parameter can have the following values: association, class, complex type, enum type, context model, method, property, relation property, enum type member;
type - this is the .NET type of the extended property values;
default - this is the value of the extended property by default;
editor - this is the editor class to be used for editing the property;
converter - this is the type to be used as a converter for the object this attribute is assigned to;
category - this is the name of the category the property is included in;
description - this is the description of the property.

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.