I don't know how many people in the garden are familiar with the T4 template language? I guess there should not be much, because I have asked several people, saying that I have heard of it, and I have heard of it. However, it is no wonder that, for me, I have just recently come into contact with T4, so my understanding of it is only superficial. I want to write this series of articles.
In fact, my original goal was to automatically generate object types, methods, and attributes through UML class diagrams when I was doing extended UML ), it uses the T4 language to write templates. So I took a moment to compare these knowledge systems.
Declaration: this series of articles is based on the introduction of MSDN T4, Master Oleg Sych's articles, as well as some practical experience or insights.
Sorry for the poor writing. If you have any questions, make more bricks and discuss more.
I. Introduction
MSDN: Code Generation and T4 Text Templates
Oleg Sych series: http://www.olegsych.com/2007/12/text-template-transformation-toolkit/
II. Introduction to T4
When I discussed this issue with my colleagues, many people didn't understand what T4 was at the beginning. In fact, T4 language, it refers to the four English letters starting with t-Text Template Transformation Tookit, that is, the Text Template Conversion Tool.
T4 template language is a template code generation language. What does it mean? Is to generate the corresponding files through the template. It is easy to understand. For example, when writing a class, we usually create a class file. At this time, its page is not blank, but has some basic content, for example, create a new MyTest class file:
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLL { class MyTest { } } |
Therefore, the common information above is created using the template language.
The extension of the T4 template language ending with. tt can be used in VS 2008 or VS 2010. For example:
We can see that the first "pre-processed Text Template" and the second is a Text Template.
Introduction to the purpose of the template in 2:
One. pre-processed Text Template
Let's talk about this first. This is the design template. You can generate text strings in the application at runtime. Pre-processed templates are sometimes called "RunTime text templates ". What is a runtime template? In fact, it is also easy to understand. For example, when we write C # code, we can also be divided into runtime compilation or design, and runtime starts when the program starts to run, in design, you compile the code at the moment of writing. For a more specific example, if we have an error or a compilation error, it will be displayed after we finish writing it. However, some errors cannot be known during compilation, for example, memory leakage, etc. This must be known only when the program is running.
In general, the pre-processing template is used in a large project because it has a partial branch class. We all know that in VS, to facilitate expansion of some elements, in most cases, use the branch class. This is no exception. In addition, the Code generated through UML class diagrams is extended through partial.
As we can see, the only difference between it and the Text Template (introduced at the second point) is that the value of the custom tool is different. The preprocessing is TextTemplatingFilePreprocessor.
Text: TextTemplatingFileGenerator
Now we will create a pre-processing file. For some of its syntaxes, we will not consider it for the time being. The syntax will be described in the next section.
A new file with the extension. tt will be created. Set the "custom tool" attribute of this file to TextTemplatingFilePreprocessor. For example:
The procedure is as follows:
1. In Solution Explorer, right-click the project, point to "add", and then click "new item ".
2. In the "Add new project" dialog box, select "pre-processing Text Template ". (In Visual Basic, under "common items \ General .)
3. type the name of the template file.
Note that the template file name is used as the class name in the generated code. Therefore, this name should not contain spaces or punctuation.
4. Click Add ". Yes, it will be OK at this step.
Then we can see only one line of code: <# @ template Language = "C #" #>
Now, put the following code below this line of code:
?
<#@ output extension=".html" #> < h1 > List all numbers: </ h2 > < table > <# for (int i = 1; i <= 10; i++) { #> < tr >< td > Number: <# = I #> </ td > < td > Multiple: <# = I * I #> </ td > </ tr > <# } #> </ table > |
Now we only know that the output is .html, extension = ". html", indicating that it outputs webpage files.
So how do we see it?
For example, if we need to call this template file in aspx, we only need to call this in the method that needs to trigger the event. For example, when I am in page_load, display the content in the template to the page.
?
protected void Page_Load( object sender, EventArgs e) { PreTextTemplate1 t4 = new PreTextTemplate1(); string str = t4.TransformText(); t4Div.InnerHtml = str; } |
Page, I put a DIV, such as <Div id = "t4div" runat = "server"> </div>, set runat as a server to be called in the background, of course, you can also use other spaces for rendering.
You can also display it on the console, and the call method is the same as above. For example, the output content on the console is:
It's cool. Try it now.
Now let's talk about text templates.
Two. Text Template
As shown in, the custom tool in the Text Template is texttemplatingfilegenerator.
A text template is a mode of display results during design, that is, the WYSIWYG mode.
1. generate an HTML file
For example, I still use the above code to create a Text Template and then write the following code:
<# @ Template DEBUG = "false" hostspecific = "false" Language = "C #" #>
<#@ Output extension = ". html" #>
<Html> <body>
<H1> Sales for Previous Month
<Table>
<# For (int I = 1; I <= 10; I ++)
{#>
<Tr> <td> Test name <# = I #> </td>
<Td> Test value <# = I * I #> </td> </tr>
<#}#>
</Table>
This report is Company Confidential.
</Body>
What is the result? You can simply expand texttemplate1.tt, and you will see a texttemplate1.html(.html is set for the outputfield of the parameter) file.
2. Generate a text file
<# @ Output extension = ". txt" #>
List numbers from 1 to 10:
<#
Int list = 10;
For (int I = 0; I <10; I ++ ){
#><#= I #><#=}#>
The only difference between it and the above is that the output is different. The viewing method is also very easy.
3. Generate class files
You can also generate class files, which are basically the same. The only difference is that the output type of output is. cs files.
<# @ Output extension = ". cs" #>
<# Var properties = new string [] {"P1", "P2", "P3" };#>
Class MyGeneratedClass {
<#
Foreach (string propertyName in properties)
{#>
Private int <# = propertyName #> = 0;
<#}#>
}
Generate
Class MyGeneratedClass {
Private int P1 = 0;
Private int P2 = 0;
Private int P3 = 0;
}
We have simulated the generation of class files. What do you think of now?
Summary
OK. Here is the basic introduction to T4. You may have some things here, such as the T4 syntax. I will explain the T4 syntax in the next section.