the previous article had its own Code generator-newlife. the xcode code generator analyzes the xcoder principle and generates code by calling the template engine quickly. It also describes the precautions before and after calling the engine, this article describes how to write templates and rules. The generated code has been posted in the previous article. Of course, this code also needs to be considered before you compile the template. It does need to be generated before you can compile the corresponding template. About the template engine description, the newlife team's big stone has a classic blog. You can refer to it. The template engine xtemplate and the Code Generator xcoder (Source Code) are more theoretical, I just want to perform the following operations.
1. Let's talk about the template engine compilation process. The following is a summary of the reference Stone:
process of compiling template engine:
1. process. Use the Regular Expression Analysis template syntax to identify commands, codes, expressions, and common texts
2. generatecode. Based on the analysis results, a C # class is generated for each template item (a template file is a template item). The result shows whether each template item has been included, the contained template items do not generate the final C # class. They are only used to be included
3, compile. Call the C # compiler to compile each C # class to form a Program set. If debugging is enabled, a dll is saved in the temporary directory xtemp. If a compilation error occurs, each C # class file is generated in the temporary directory, which facilitates errors
4 and render. After compilation, the template engine is ready. By calling a template to generate code, you can find the type generated by the template and create an instance. This instance naturally inherits from templatebase. Then input the parameter and call the render method. The return value of render is the string result. Almost all of the code generated by the template is in the render method, which is mainly a stringbuilder that continuously concatenates various content.
it is a very time-consuming process to analyze, generate template code, and compile the template engine, so it cannot be repeated.
xtemplate has a complete caching mechanism to ensure that only one compilation is performed. Then, each render is the standard C # code execution, surpassing all non-compiled template engines.
2. Let's talk about the template syntax. In fact, this syntax is quite common and simple. There are only a few key points, but the implementation behind it may not be that simple:
1. The template uses C # as the template language, and the text content outside the tag is output as a string as is, the <##> label is executed as the C # code, so it is easy to understand.
2. The data value obtained in the template engine. The imported data is a dictionary set and can be accessed by name. Any obtained variable value must be <#=#>, for example, <# = data ["namespace"] #>, obtain the value of the key in the dictionary as namespace and use it as the namespace. <###> You can also obtain the variable values defined in the template.
3. The variable definition in the template is defined in <##>. Do not set the variable to "no. =". You can use <#=#> to replace the partial score. For example
<# String title = <# = data ["title"] #> name #>, which combines a string and can be called as follows <#= title #>
3. In addition, the loops required in the template must be included in <##>, and all other loops in <##> are output as they are.
4. we can see that the entire template contains the two things <##>and <#=#>. They are the key. Although they are not difficult, they are indeed troublesome because of errors, it is difficult to modify. Of course, the template engine has debugging functions. Of course, if you write more and modify more, the experience will be easy. The following is a simple template that defines fields. Field definitions are generated based on data as a set of key-value pairs. Key-value pairs are field names and value is field types. There are two other fields in the data set, which cannot be included, namely namespace and classname. Therefore, these two fields must be excluded during field generation, so there is a judgment in the following loop.
View code
1 Namespace <# = Data [ " Namespace " ] #>
2 {
3 Public Class <# = Data [ " Classname " ] #>: Wmibase
4 {
5 # Region Field Definition
6 <# Foreach (Keyvaluepair < String , Object > Item In Data)
7 { If ((! Item. Key. Contains ( " Namespace " ) | (! Item. Key. Contains ( " Classname " )))
8 {#>
9 Private <# = Item. Value #>_< # = item. Key. tolower () #>;
10 <#}
11 } #>
12 Private Managementobject _ Mo;
13 # Endregion
The above code generates the field definition. Comparing the code generated in the previous blog is the process of loop splicing.
Of course, the template engine also has a powerful function, that is, debugging, which can detect some potential errors, making it easier to detect and modify errors. To illustrate the code above, I have prepared a diagram of the description. If you don't understand it, you just need to do something simple and you will understand it. Is it practical. First, let's compare the template description with the production code description:
Check the generated code.
As for the incoming data, we have mentioned in the previous analysis of the Code Generator newlife. xcode code generator. The process should be clear. If you have any questions, you can raise them for discussion.
Information about the New Life Development Team, QQ group: 1600800
Blog: http://nnhy.cnblogs.com
Forum: http://www.53wb.com Open Source: http://xcode.codeplex.com/