Depth analysis of fastm design idea

Source: Internet
Author: User
Tags contains variables php code php template xslt

1. PHP (&fastm) to cut the document into a simple DOM structure

PHP templates are designed to be very beautiful, using the begin and end of the annotation to cut the HTML (WML, or any XML) pages into different blocks, and the blocks can continue to be diced.

As a result, a page is cut into a tree structure, much like a DOM structure. It's just that the DOM structure is too cumbersome to create a node for each element, and the type of the node is very complex. For example, an HTML DOM structure, how many HTML elements there are, how many types of nodes, such as Body,table,tr,td,form,input.

The PHP template is a lightweight DOM structure, and a begin-end block is a node. The Begin-end block consists of only three kinds of content-static text, variables, and other begin-end blocks.

Design ideas are so simple and powerful, easy to use and generic (can be used in any spec or nonstandard XML page, such as HTML,WML, or even xul,xaml), and can be seen in the HTML editor. Throughout the world template technology, MO out its right.

I have been through a variety of Java page technology torture, introduced by colleagues, know the PHP template technology, happy abnormal, originally unexpectedly have such good things, only hate acquaintance too late.

Fastm template ideas completely draw on the idea of PHP template, just slightly done some expansion. (For more information, see my previous article on Jdynamite,--java Page Technology Overview).

The begin-end dynamic block of the fastm template is equivalent to the begin-end block of the PHP template.

The begin-end ignored block of the fastm template is equivalent to the Begin-end block that is ignored by the PHP template.

For example, the following HTML fragment.

<select name= "ZipCode" >

<!--BEGIN Dynamic:zipcodes-->

<option value= "" ></option>

<!--end Dynamic:zipcodes-->

</select>

We see that this fragment contains a begin-end block (zipcodes), which contains two identical variables, and the rest is static text.

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

Static text <select name= "ZipCode" >

Dynamic Block ZipCodes--

| ---static text <option value= "

| ---variables

| ---static text ">

| ---variables

| ---static text </option>

Static text </select>

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 not changed.

PHP code loads a php template at a time, and then dynamically replaces the value of the variable part inside. PHP templates are essentially readable and can be manipulated to change.

The HTML (Wml,xml) DOM is more so. The program directly modifies the value of the DOM node to get different dynamic results. It can be said that the XML DOM is inherently used to manipulate changes. The XML Dom itself is also a template and data.

The DOM structure that can be changed cannot be used in multi-threaded environments. Each thread must obtain its own fresh DOM backup, make operational changes, and get its own dynamic results. Think about how much space and time wasted in a DOM structure where static text accounts for most of it.

The DOM structure of the fastm template is read-only and cannot be changed. So a fastm dom can be used in a multithreaded environment.

Now that we can't change the fastm Template dom, how do we assign fastm Template dom? How do we use the Fastm Template dom to get dynamic results?

Fastm introduced the concept of Valueset. Valueset is a tree-structured dynamic dataset that matches a read-only fastm template DOM structure to generate dynamic results.

The programmer must construct the entire tree dynamic dataset (Valueset DOM) beforehand, and then combine the Valueset dom and the fastm Template DOM to generate dynamic results.

So, the whole use of fastm is as follows:

(1) During the entire operation of the program, the fastm template file (that is, an HTML file with begin-end annotations) needs to be parsed once, generating a fastm Template DOM.

(fastm template parsing speed is very fast, more than JSP compilation, Velocity parsing, XML Dom parsing, are much faster, in most cases even faster than sax parsing.) And the Fastm Dom is almost as big as the original fastm template file, with just one more list recording different blocks and more space efficient.

(2) The program generates different Valueset DOM, matching the read-only fastm dom to produce different dynamic results.

(due to the simplicity and efficiency of the fastm Template DOM structure, the entire matching process is fast.) Typically, time efficiency is even higher than the fastest pure JSP or servlet. The Valueset DOM is less space efficient than a pure JSP or servlet, but is reasonably reusable, at least close to the space efficiency of a pure JSP or servlet. The next Advanced Application topic series will explain the issue in detail. )

For example, we construct a valueset dom for the above template DOM structure (ZipCode Select).

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

Ivalueset top = new Valueset (); corresponding to the entire HTML fragment above

List items = new ArrayList (); corresponding 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 top this valueset dom with the template DOM. The following results are generated.

<select name= "ZipCode" >

<option value= "361005" >361005</option>

<option value= "10008" >100008</option>

</select>

We can see that there is not a one by one correspondence between the Template DOM node and the Valueset DOM node, but rather a one-to-many relationship. A template DOM node corresponds to a valueset List. The Valueset list contains a number of valueset, and the template DOM node shows less time.

The advantages of fastm are obvious compared to the taglib. Fastm a few lines of code, or a method, that can implement one or several taglib functions. Compared to any other page technology, other page technology can do, or do a good job, fastm can do, and do better. And fastm can do a lot of other page technology can't do things. All right. I don't have much to say. J

The separation of Valueset dom and template dom is a great way of thinking about innovation and leaps.

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

The separation of the Valueset Dom from the template DOM is an unprecedented and thorough display of data. More thorough than the Xml/xslt method. XML is really pure data, but it is inevitable to include logic in XSLT. The Valueset Dom is pure data, without any logic, the Template dom is a pure display template, and there is no logic.

A copy of the template Dom can be assigned with multiple Valueset dom. Similarly, a valueset DOM can also be used with multiple template dom to display the same data in different styles of templates.

For example, we have such an HTML fragment:

<table>

<!--BEGIN Dynamic:zipcodes-->

<tr><td></td></tr>

<!--end Dynamic:zipcodes-->

</table>

Let's assign the top valueset to this template. The results obtained are as follows.

<table>

<tr><td>361005</td></tr>

<tr><td>100008</td></tr>

</table>

As we can see, the Template Dom is a template that contains only display styles and chunking definitions. The Valueset DOM is data and contains only data.

FASTM has incomparable advantages over other page generation technologies:

What you see is what you get, the complete separation of the template from the data, the multiple pairs of templates and data to match, easy to learn and use, fast development, small running space, and fast running speed.

In my personal sense, fastm is simply a silver bullet that solves all the technical problems of the page.

There may be some "puff" above, which is explained as follows:

I am not a man of boast. Otherwise have been engaged in salesmen, public relations and other promising career. Moreover, due to the lack of self-promotion and social relations, it has already caused a great waste of its ability, energy and time. I'm miserable about my situation, but I don't regret it. I understand the social rules of the game, but do not have the ability, do not want to, disdain to participate. There is always a wishful thinking in mind, hoping to create a miracle that is free from the existing rules.

In addition, I am an objective and realistic, clear-minded person, with "out of the enemy, not to avoid the son," the responsible academic spirit. There is a point, when said a point, never say a point, and never less say one point.

3. The core of Fastm reusability is the Valueset DOM

The key to reusability of JSP technology lies in Taglib.

The core of the reusability of XML Dom lies in the common operation of DOM nodes.

The core of Fastm reusability is the common operation of Valueset DOM nodes. For example, the section above generates Valueset code.

The Template DOM itself can be used as a read-only template, and any Template DOM structure included under the Template DOM can also be used as a stand-alone read-only template. This is the same as the XML DOM. Any node of the XML Dom can be used as a separate node.

Valueset Dom is not only the core of data reuse, but also the backbone of template assembly reuse. Fastm to achieve the various blocks between the handling of the assembly, it is easy. Realize the so-called tile function, a piece of cake.

JSP, and some taglib,velocity templates that represent logic, XSL files, are templates that contain logic. The individual believes that the template contains logic, is a very funny behavior. The advantage of a template is that it shows the layout of the page, the display style, not the logic. Why not let the logic of Java to deal with logic?

In Fastm, neither the Template Dom nor the Valueset DOM contains any logic. All the logic falls into the Java code. While Java is a highly object-oriented language, its structural and reusability is unmatched by any template language. Therefore, the core of fastm reusability is the common operation of Valueset DOM nodes. The code for these generic operations is, of course, 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.