In-depth analysis of fastm design ideas

Source: Internet
Author: User
1. PHP (fastm) cut the document into a simple DOM structure. the design idea of the PHP template is very beautiful. Use Begin and End in the comment to put HTML (WML, or any XML) the page is cut into different blocks, and the blocks can be further cut. In this way, a page is cut into a tree structure, like a DOM structure. The DOM structure is too cumbersome. for every element, we need to create 1.php (& fastm) to cut the document into a simple DOM structure.

The PHP template design idea is very beautiful. use the Begin and End in the annotation to cut the HTML (WML, or any XML) page into different blocks, and you can continue to cut the blocks.

In this way, a page is cut into a tree structure, like a DOM structure. The DOM structure is too cumbersome. a node must be created for each element, and the node type is very complex. For example, if an html dom structure contains multiple types of HTML elements, such as Body, Table, TR, TD, Form, and Input.

The PHP template is a lightweight DOM structure, and a Begin-End block is a node. The Begin-End block contains only three types of content: static text, variables, and other Begin-End blocks.

The design concept is so concise, powerful, easy to use, and Universal (it can be used in any standardized or nonstandard XML pages, such as HTML, WML, or even XUL, XAML), and, what you see is what you get in the HTML editor. Throughout the world of template technology, do not find its right.

After suffering from various Java page technologies, I was introduced by my colleagues to understand the PHP template technology. I was so excited that it was too late to get acquainted with such a good thing.

The idea of Fastm template fully draws on the idea of PHP template, but it just makes some extensions. (For details, see my previous article about JDynamiTe-Java page Technology overview ).

The BEGIN-end dynamic Block of the Fastm template is equivalent to the PHP in-End block of the PHP template.

The BEGIN-end ignored block of the Fastm template is equivalent to ignoring the hidden Begin-End block in the PHP template.

For example, the following HTML snippet.

             

We can see that this segment contains a BEGIN-END block (zipcodes), which contains two identical variables, and the other parts are static text.

The fastm Template DOM structure of this piece is as follows:

Static textDynamic chunk zipcodes --| --- Static text| --- Variable| --- Static text ">| --- Variable| --- Static textStatic text

2. fastm's ValueSet is another leap in the DOM concept.

One of the core features of the Fastm template DOM structure is that it can only be read and cannot be changed.

Each time the PHP code loads a PHP template, it dynamically replaces the value of the variable section. In essence, PHP templates can be read and changed.

This is especially true for HTML (WML, XML) DOM. The program can directly modify the value of the DOM node to obtain different dynamic results. It can be said that xml dom is inherently used to operate changes. Xml dom is a template and data.

The DOM structure that can be changed cannot be used in a multi-threaded environment. Each thread must obtain its own fresh DOM backup, perform operation changes, and obtain its own dynamic results. Think about it. in the DOM structure where a static text occupies the vast majority, this will result in a waste of space and time.

The DOM structure of the Fastm template is read-only and cannot be changed. Therefore, a Fastm DOM can be used in a multi-threaded environment.

Since we cannot change the Fastm Template DOM, how do we assign values to the Fastm Template DOM? How can we use Fastm Template DOM to obtain dynamic results?

Fastm introduces the concept of ValueSet. ValueSet is a dynamic dataset with a tree structure. it is used to match the read-only Fastm template DOM structure and generate dynamic results.

The programmer must construct the entire tree-like dynamic dataset (ValueSet DOM) in advance, and then combine it with the ValueSet DOM and Fastm Template DOM to generate dynamic results.

Therefore, fastm is used as follows:

(1) during the entire running process of the program, the fastm Template File (that is, the HTML file with the BEGIN-END annotation added) only needs to be parsed once to generate a Fastm Template DOM.

(Fastm template parsing is faster than JSP compilation, Velocity parsing, and xml dom parsing. In most cases, it is faster than SAX parsing. The size of the fastm DOM file is almost the same as that of the original fastm template file. only one List is added to record different blocks, and the space efficiency is higher)

(2) the program generates different ValueSet DOM, matches the read-only Fastm DOM, and generates different dynamic results.

(Because the fastm Template DOM structure is simple and efficient, the entire matching process is fast. Generally, the time efficiency is even higher than the fastest pure JSP or Servlet. The space efficiency of ValueSet DOM is inferior to that of pure JSP or Servlet, but after reasonable reuse, it can at least be close to the space efficiency of pure JSP or Servlet. This issue will be explained in detail in future advanced application topic series .)

For example, we construct a ValueSet DOM for the above Template DOM structure (zipcode Select.

String [] zipcodes = {"361005", "100008 "};

IValueSet top = new ValueSet (); // corresponds to the entire HTML clip above

List items = new ArrayList (); // corresponding to the dynamic part zipcodes

For (int I = 0; I <zipcodes. length; I ){

IValueSet item = new ValueSet ();

Item. setVariable ("", zipcodes [I]);

Items. add (item );

}

Top. setDynamicValueSets ("zipcodes", items );

We combine the ValueSet DOM of top with the Template DOM. The following result is generated.

     361005   100008  

We can see that the relationship between the Template DOM node and the ValueSet DOM node is not one-to-one, but one-to-many. A Template DOM node corresponds to a ValueSet List. The number of valuesets contained in the ValueSet List. this Template DOM node shows the number of times.

Compared with TagLib, fastm has obvious advantages. Several lines of fastm code or a method can implement one or several TagLib functions. Compared with any other page technology, other page technologies can achieve, or do well, fastm can do, and do better. Fastm can do a lot of things that other page technologies cannot do. Okay. Not much. J

The separation of ValueSet DOM and Template DOM is a great innovation and leap in thinking.

After all, the dynamic part of the page, compared with static, is a very small part. ValueSet DOM represents the dynamic part, which is generated by the program at any time and can exist in multiple copies. Template DOM indicates the static part, which only needs to be parsed once and only needs one copy.

The separation of ValueSet DOM and Template DOM is an unprecedented and thorough separation of display and data. It is more thorough than XML/XSLT methods. XML is indeed pure data, but it is inevitable to include logic in XSLT. ValueSet DOM is pure data without any logic. Template DOM is pure display Template without any logic.

One Template DOM can be assigned with multiple ValueSet DOM values. Similarly, a ValueSet DOM can also be used for multiple Template DOM to display the same data in templates of different styles.

For example, we also have an HTML clip:

  

  

  

  

  

We will assign the top ValueSet to this template. The result is as follows.

  

  

  

361005
100008

We can see that the Template DOM is a Template that only contains the display style and block definition. ValueSet DOM is data and only contains data.

Fastm is superior to other page generation technologies:

What you see is what you get. templates and data are completely separated. templates and data can be matched to each other in many-to-many ways. they are easy to learn and use. the development speed is fast, the running space is small, and the running speed is fast.

In my personal sense, fastm is a silver bullet to solve all the page technical problems.

The above may be a bit of "self-selling", which is explained as follows:

I am not a boast. Otherwise, he would have been engaged in promising careers such as salesman and PR propaganda. In addition, the lack of self-promotion ability and social relationship learning ability has resulted in a great waste of ability, energy and time. I am suffering from my own status quo, but I do not regret it. I understand the rules of games in society, but I am incapable and unwilling to participate. There is always a kind of delusion in the Inner's mind, hoping to create a miracle beyond the existing rules.

In addition, I am an objective, realistic, and clear-minded person. I am responsible for the academic spirit of "taking actions outside of the world, taking actions inside the world. If you say one point, you will never say more or more.

3. the core of fastm reusability lies in the ValueSet DOM.

The core of JSP technology reusability lies in TagLib.

The core of the reusability of xml dom lies in the common operations of DOM nodes.

The core of Fastm's reusability lies in the general operation of the ValueSet DOM node. For example, the code for generating ValueSet described above.

The Template DOM itself can be used as a read-only Template. Similarly, any Template DOM structure contained in the Template DOM can also be used as an independent read-only Template. This is the same as xml dom. Any node of the xml dom can be used as an independent node.

ValueSet DOM is not only the core of data reuse, but also the hub for template assembly and reuse. Fastm makes it easier to assemble the blocks between various templates. Implement the so-called Tile function.

JSP, and some TagLib, Velocity templates and XSL files that represent logic are all templates that contain logic. In my opinion, the template contains logic, which is a ridiculous behavior. The strength of a template lies in the layout and display style of the page, rather than the logic. Why not let Java, which is good at logic, process logic?

In fastm, neither Template DOM nor ValueSet DOM contains any logic. All logic falls into Java code. Java is a highly object-oriented language, and its structure and reusability are unmatched by any template language. Therefore, the core of the reusability of fastm lies in the general operation of the ValueSet DOM node. Of course, the code for these general operations is implemented by Java.

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.