Template engine xtemplate and code generator xcoder (source code)

Source: Internet
Author: User

The template engine xtemplate is an engine similar to the T4 design. It basically functions the same as T4 (the template syntax is fully compatible with T4, And the template header instruction is partially compatible ).

You can design a template engine for multiple scenarios, such as code generators, website templates, and email templates. That is, you must be able to use it 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 for one compilation (all template classes are compiled into one assembly ).

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, where the ordinal number is singular and the ordinal number is an even number. Therefore, the position of the class member code is not restricted (T4 must be written at the end of the template ).

6. You can customize the base class. 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), such as the xcoderbase in the Code Generator xcoder.

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 compilation, xtemplate automatically references all application assembly of the host (that is, the caller, such as xcoder) and references most of the commonly used explicit space. Because of this, the generated class is 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-domain. 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!

 

Xcoder uses the xtemplate Code (the xcoder project code is later ):

Dictionary <string, Object> DATA = new dictionary <string, Object> (); Data ["Config"] = config; data ["tables"] = tables; data ["table"] = table; // declare 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 the 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 = pa Th. 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; filename = 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 diagram (I like to do the image first and then code it ):

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.