What is the difference between <# and <#+ in T4?
Asked Answered
I

3

18

What is the difference between the <# tag and the <#+ tag in T4?

Ichnography answered 2/8, 2010 at 3:12 Comment(0)
L
19

The docs explain:

Standard control blocks

A standard control block is a section of program code that generates part of the output file.

You can mix any number of text blocks and standard control blocks in a template file. However, you cannot place one control block inside another. Each standard control block is delimited by the symbols <# ... #>.

...

Class feature control blocks

A class feature control block defines properties, methods, or any other code that should not be included in the main transform. Class feature blocks are frequently used for helper functions. Typically, class feature blocks are placed in separate files so that they can be included by more than one text template.

Class feature control blocks are delimited by the symbols <#+ ... #>

For example, the following template file declares and uses a method:

<#@ output extension=".txt" #>
Squares:
<#
    for(int i = 0; i < 4; i++)
    {
#>
    The square of <#= i #> is <#= Square(i+1) #>.
<#
    } 
#>
That is the end of the list.
<#+   // Start of class feature block
private int Square(int i)
{
    return i*i; 
}
#>
Louie answered 2/8, 2010 at 3:21 Comment(0)
I
11

Reading the documentation doesn't immediately make it obvious what the difference is. They're both code that's included in your generated template.

This article archive makes it a little more clear.

To see the difference in action, create a Runtime Text Template with these contents:

<#@ template language="C#" #>
<# // STANDARD CONTROL BLOCK #>
<#+ // CLASS FEATURE BLOCK #>

The generated class will look essentially like this:

public class Something
{
    public string TransformText()
    {
 // STANDARD CONTROL BLOCK 
        return this.GenerationEnvironment.ToString();
    }
 // CLASS FEATURE BLOCK 
}

As you can see, standard control blocks are placed in the TransformText method, while class features are placed at the class level.

Iscariot answered 2/1, 2014 at 2:28 Comment(3)
This should be the accepted answer. Contrary to the MSDN docu, this makes the actual difference quite clear. Thanks for the info!Frontal
The link to the article is dead.Vitriolic
@DrewChapin Added a web.archive.org link. Thanks!Iscariot
D
4

A class feature control block is a block in which you can define auxiliary methods. The block is delimited by <#+...#> and it must appear as the last block in the file. Ref

Delogu answered 2/8, 2010 at 3:22 Comment(1)
The 'last block in the file' point is noteworthy. After you've used a <#+ #> block in a particular template file, you can no longer use regular <# #> blocks within that file. Consequently, your helpers/template members/nested classes need tob e grouped either at the end of a template file, or more frequently (for reuse) placed in a separate file and included.Submerse

© 2022 - 2024 — McMap. All rights reserved.