Template engine XTemplateAnd Code GeneratorXCodeIs the content to be introduced in this article, mainly to learnTemplate engine XTemplateIt is an engine designed to simulate T4 and is basically functionally consistent with T4. The template syntax is fully compatible with T4, And the template header instruction is partially compatible ).
Self-designTemplate engineIt is used for various scenarios such as code generators, website templates, and email templates, that is, it must be used independently, with powerful functions and easy to control. T4 is a good engine, but its design is basically in favor of vs, almost regardless of other occasions.
XTemplate has the following features:
1. Use C # as the template language. With ASP, ASP. like parsing on the Net page, the text content outside the tag is treated as a string, and a StringBuilder is used, and the tag is used as the C # native code and compiled together, during template replacement, the compiled assembly is actually executed. This is the core principle of XTemplate! Many existing template engines on the network either use tag replacement or create a template language, which increases the learning difficulty of users. XTemplate uses C # as the template language. The world is quiet!
2. Support for "debugging ". Instead of debugging at runtime, XTemplate can output intermediate class files compiled by the template and Assembly to facilitate error check. If you save the compiled assembly of the template, you can directly use the template function without a template file.
3. ASP. Net is not required. Some template engines simulate an ASP. net Server, and then use ASP. net as a template, which requires an ASP.. Net Server as the host, which limits the use scope of the template engine.
4. Batch compilation is supported. You can put multiple templates into the template processor and compile all template classes into one assembly at a time ).
5. Support class members. By default, the template content will be compiled into the Render method of a class. However, sometimes we need to add some attributes and methods to this class. You can use the <#! #> Tag. If the ordinal number is singular, it indicates start. If the ordinal number is an even number, it indicates end. Therefore, the position of the class member code is not restricted. T4 must be written at the end of the template ).
6. Custom base classes are supported. By default, all template classes generated by compilation are inherited from TemplateBase. You can also create your own template base classes and run the commands in the template header, you can also use an external host to specify the Custom template base class. The template can directly use the members of the Custom template base class because of inheritance. For example, the XCodeBase in the Code Generator XCode.
7. Automatically reference the host assembly. The biggest trouble with using T4 is to reference the external assembly and namespace. After all, C # code is not written in. During the compilation of XTemplate, the host is automatically referenced by the caller, for example, all application sets of XCode, and most of the commonly used explicit spaces are referenced. Because of this, the generated classes are very bloated, however, during compilation, the compiler automatically removes useless references. XTemplate has not referenced the Assembly and namespace since its completion. Generally, the Assembly used in the template is useful in the host, it is very consistent with our usage habits.
8. Good interaction with the host. In XTemplate, the compiled template assembly is directly loaded into the default domain. Unlike T4, T4 creates a new domain, it should be to prevent the template code from dirty data in the default domain, such as interfering with vs running ). Because XTemplate interacts with the host in the same domain, you do not need to "***" cross-origin. The processing process of XTemplate can be divided into three steps: analysis, compilation, and execution. For example, sometimes we only need to check the template syntax and check whether the template syntax is correct, compile it at this time.
9. More features need to be discovered!
XCode uses the XTemplate code followed by the XCode project code ):
- Dictionary <String, Object> data = new Dictionary <string, object> ();
- Data ["Config"] = Config;
- Data ["Tables"] = Tables;
- Data ["Table"] = table;
-
- // Declare the template engine
- Template tt = new Template ();
- Template. Debug = Config. Debug;
- Foreach (String item in ss)
- {
- If (item. EndsWith ("scc", StringComparison. Ordinal) continue;
-
- String tempFile = item;
- If (! Path. IsPathRooted (tempFile )&&! TempFile. StartsWith (TemplatePath, StringComparison. OrdinalIgnoreCase ))
- TempFile = Path. Combine (TemplatePath, tempFile );
-
- String content = File. ReadAllText (tempFile );
-
- // Add a File Header
- If (Config. UseHeadTemplate &&! String. IsNullOrEmpty (Config. HeadTemplate ))
- Content = Config. HeadTemplate + content;
-
- Tt. AddTemplateItem (item, content );
- }
- Tt. Process ();
-
- // Compile the Template
- Tt. Compile ();
-
- List <String> rs = new List <string> ();
- Foreach (String item in ss)
- {
- If (item. EndsWith ("scc", StringComparison. Ordinal) continue;
-
- // String content = RenderFile (table, item, data );
- String content = tt. Render (item, data );
-
- // Calculate the output file name
- String fileName = Path. GetFileName (item );
- String className = CutPrefix (table. Name );
- ClassName = FixWord (className );
- String remark = table. Description;
- If (String. IsNullOrEmpty (remark) remark = ENameToCName (className );
- If (Config. UseCNFileName &&! String. IsNullOrEmpty (remark) className = remark;
- FileNamefileName = fileName. Replace ("Class Name", className). Replace ("Class description", remark). Replace ("connection name", Config. EntityConnName );
-
- FileName = Path. Combine (OuputPath, fileName );
- File. WriteAllText (fileName, content, Encoding. UTF8 );
-
- Rs. Add (content );
- }
XTemplate Design DrawingI like to do the image first and then encode it.) Click to zoom in:
Summary: DetailsTemplate engine XTemplateAnd Code GeneratorXCodeSource code), I hope this article will help you!