[ASPX] template engine xtemplate and code generator Xcoder (source)

Source: Internet
Author: User

Template engine xtemplate is an imitation T4 design engine, functionally basic and T4 consistent (template syntax is fully compatible with T4, module masthead instructions are partially compatible).

Their own design template engine, is for the code generator, website template, mail template and other occasions, that is, to be able to take out alone, powerful and easy to control. T4 is a good engine, but its design tends to be mostly vs, almost regardless of other occasions.

Xtemplate features are as follows:

1, fully use C # as the template language. With ASP, ASP. NET page parsing, the text content outside the <##> tag as a string, with a StringBuilder, the tag as C # native code, spelled together to compile, for template replacement, is essentially the implementation of the compiled assembly, This is the core principle of xtemplate! Many of the existing template engines on the Web, either tagged replacement or self-creating template languages, add to the learning difficulty of the user. Xtemplate use C # as a template language, the world is quiet!

2, support "Debug". Instead of debugging at run time, Xtemplate is able to output the intermediate class files and assemblies that the template compiles to make it easier to check for errors. If you save the template compiled assembly, you can use the template function directly without a template file.

3, no ASP support is required. A partial template engine, which simulates an ASP. NET Server, is then implemented as a template, which requires an ASP. NET Server as the host, limiting the use of the template engine.

4, Support batch compilation. You can put multiple templates into the template processor and compile it once (all template classes are compiled into one assembly).

5, supports class members. Template content will be uniformly compiled into the render method of a class by default, but sometimes we need to add some properties and methods to this class, you can use <#! #> tags, the ordinal number is the beginning of the singular, the ordinal number is an even number to end, so do not limit the location of the class member code (T4 requirements can only be written in the final face of the template).

6, supports custom base classes. By default, all compiled template classes are inherited from Templatebase, you can also create your own template base class, and then specify a custom template base class in modulo masthead through directives, or through an external host, where you can use the members of the custom template base class directly (because of inheritance). such as the xcoderbase in the code generator Xcoder.

7, automatically referencing the host assembly. The biggest trouble with T4 is referencing external assemblies and namespaces, not writing C # code in VS. Xtemplate at compile time, automatically refer to the host (that is, the caller, such as Xcoder) all the application set, while referencing most of the usual clear space, because the generated class is very bloated, but compile, the compiler will automatically remove the useless reference. Xtemplate the problem of referencing assemblies and namespaces has not been used since completion, because in general, the assemblies that need to be used in the template are generally useful in the host, and are very much in line with our usage habits.

8, good interaction with the host. In Xtemplate, the compiled template assembly is loaded directly into the default domain, and unlike T4, T4 creates a new domain, which should be designed to prevent the template code from staining the default domain data (such as interference vs run). Because Xtemplate interacts with the host in the same domain, there is no need for "FQ" (cross-domain). Xtemplate processing process is divided into analysis, compilation and execution of three steps, can be external control, such as sometimes we just need to check the template syntax, only need to check the template syntax is correct, this time to compile a bit.

9, more features need everyone to discover!

Xcoder uses the Xtemplate code (the project code that follows Xcoder):

dictionary<String,object> data =Newdictionary<StringObject> ();d ata["Config"] = config;data["Tables"] = tables;data["TABLE"] = table;Declaring the template engineTemplate TT =NewTemplate ();Template.debug = Config.debug;foreach (String Itemin SS) {if (item. EndsWith ("SCC",stringcomparison.ordinal))ContinueString tempfile = Item;if (!Path.ispathrooted (tempfile) &&!tempfile.startswith (TemplatePath,stringcomparison.ordinalignorecase)) Tempfile =Path.Combine (TemplatePath, tempfile);String content =File.readalltext (Tempfile);Add a file headerif (config.useheadtemplate &&!)String.IsNullOrEmpty (config.headtemplate)) content = config.headtemplate + content; Tt. Addtemplateitem (item, content);} Tt. Process ();Compiled template tt.compile ();list<String> rs =Newlist<String> ();foreach (String Itemin SS) {if (item. EndsWith ("SCC",stringcomparison.ordinal))ContinueString content = renderfile (table, item, data);String content = TT. Render (item, data);Calculate output File nameString 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; FileName = Filename.replace ( "class name", ClassName). Replace (file.writealltext (fileName, content, encoding.utf8); Rs. ADD (content),}             

Xtemplate design diagram (I like to do the diagram before coding):


      • 1, log Component Xlog
      • 2,orm component Xcode (quick glance)
      • 3,orm component Xcode (Introduction)
      • 4, Dance with ObjectDataSource
      • 5,orm component Xcode (Hands-on)
      • 6,orm component Xcode (pry tens data)
      • 7,orm component Xcode (terrorize)
      • 8, Protocol serialization component Newlife.serialization
      • 9, template engine Xtemplate and code generator Xcoder (source)

      • Performance & Distributed &newlife.xcode support for unlimited data
      • Improved version Codetimer and Xcode performance test
      • New Life Development Team ORM Framework Xcode v3.5.2009.0714 source release (Christmas gift package)


    • Data Mapping framework Xcode open Source Address http://xcode.codeplex.com

[ASPX] template engine xtemplate and code generator Xcoder (source)

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.