User Tools

Site Tools


t4_templates.htm
Navigation:  T4 Template Language Reference >Overview Previous pageReturn to chapter overviewNext page

T4 templates consist of processing directives, text blocks and code blocks.  There are three template file types and corresponding file extensions; the extension .TTL contains a list of one or more templates that comprise a template chain, the extension .TTN is used for an “include” file, and the extension .TT is a template file that is part of a template chain.

Processing Directives

The T4 templates use an ASP style syntax.

Template Directive

<;#@ template language='C#' debug='true' type='Utility' name = 'DCTTreeWizard' chain='Clarion.File'#>

Assembly Directive

<;#@ assembly name=“[assembly file name]” #>

Property Directive

<;#@ property name=“BrowseTable” type=“System.String” editor=“SoftVelocity.DataDictionary.Design.ITableTypeEditor”#>

T4 Template Helper function

<;#+ [method][field declaration][property declaration]; #>

<;#@ DirectiveName [ParameterName = “ParameterValue”] #>

Line 14 is interesting: <;#+ string _OutputFile = '';#>

the <;#+ tag indicates a template helper block. The helper block allows the template developer to add code at the TextTransformation Class level. The code added can be a Method, a Field, or a Property declaration.

If a Method is declared within the helper block, it can be compared to a #GROUP from our Win32 template language. Embeds can also be declared inside a helper block.

for example:

<;#+ void MyPseudoGroup() {#> <;#% MyEmbed #> <;#+}#>

In this case when the method MyPseudoGroup is called, the content of the MyEmbed will be also executed.

Line 15: <;# this._OutputFile = this.OutputFile;#>

just the value set in the 'OutputFile' within the property directive to the internal _OutputFile variable.

Line 16: <;# Create(this._OutputFile);#>

calls an internal method that creates a file on disk to receive the generated text (code or other text). It corresponds to our Win32 counterpart #CREATE.

Line 17:

*

is a text block, as there are no template directives surrounding it. Text blocks are copied to the output 'as is', with no further processing.

Line 18: ' Listing for Dictionary: <;#= this.DataDictionary.FileName #>

mixes a text block: '- Listing for Dictionary: ' with an expression block:

<;#= this.DataDictionary.FileName #>

You use expression blocks in text templates to add strings to the generated text output. In this case we're asking for the name of the Dictionary that we're processing. Expression blocks are delineated by using the <;#= template tag. The syntax is: <;#= ExpressionCode #>

Lines 19-49 use code blocks. The syntax for a code block is: <;# Code block #>

Code blocks can generate template output. In our template the code block uses a few nested foreach loops to walk the dictionary. We're using a mix of text blocks and expression blocks to write the details of the dictionary to the output file. Code blocks can use any available .NET APIs. In this template we're using the DataDictionary API to output the Dictionaries collections and properties; Tables, Columns, Keys and Relationships.

I hope this walk-through has provided a good introduction to the new template syntax used in AppGen.Net.  For those who are thinking of writing some T4 templates the attached example is a good starting point to get familiar with the essentials. Next time we'll go a bit deeper into the template syntax.

Enter topic text here.

t4_templates.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1