Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

t4

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

like image 248
Earlz Avatar asked Aug 02 '10 03:08

Earlz


3 Answers

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; 
}
#>
like image 191
Mark Rushakoff Avatar answered Oct 16 '22 19:10

Mark Rushakoff


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.

like image 8
Kendall Frey Avatar answered Oct 16 '22 21:10

Kendall Frey


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

like image 4
Mitch Wheat Avatar answered Oct 16 '22 19:10

Mitch Wheat