开发者

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

开发者 https://www.devze.com 2023-01-10 00:19 出处:网络
W开发者_如何学编程hat is the difference between the <# tag and the <#+ tag in T4?The docs explain:

W开发者_如何学编程hat is the difference between the <# tag and the <#+ tag in T4?


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; 
}
#>


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消