fastm设计思路深度剖析_PHP
1.PHP(&fastm)把文档切割为简单的DOM结构
PHP模板的设计思路非常漂亮,用注释里的Begin和End把HTML(WML,或任何XML)页面切割成不同的块,而且块里面还可以继续切块。
这样一来,一个页面被切割成一个树结构,很象DOM结构。只是DOM结构太过笨重,对每一个元素都要建立一个节点,而且节点的类型非常复杂。比如,一个HTML DOM结构,有多少种HTML元素,就会有多少种节点类型,比如,Body,Table,TR,TD,Form,Input等。
而PHP模板则是一个轻量级的DOM结构,一个Begin-End块就是一个节点。Begin-End块只包括三种内容——静态文本,变量,和其它的Begin-End块。
设计思路如此简洁而强大,易用而通用(可以用在任何规范或不规范的XML页面中,比如HTML,WML,甚至XUL,XAML),而且,能够在HTML编辑器中所见即所得。纵观天下模板技术,莫出其右。
我经过了多种Java页面技术的折磨,经同事介绍,认识了PHP模板技术,欣喜异常,原来竟有这样的好东西,只恨相识太晚。
Fastm模板的思路完全借鉴PHP模板思路,只是稍微做了一些扩展。(详情请参见我的上一篇提到JDynamiTe的文章——Java页面技术综述)。
Fastm模板的BEGIN-END DYNAMIC块,就相当于PHP模板的Begin-End块。
Fastm模板的BEGIN-END IGNORED块,就相当于PHP模板的忽略不显示的Begin-End块。
比如下面的HTML片断。
我们看到,这个片断包含一个BEGIN-END块(zipcodes),这个块里包含两个相同的变量,其它的部分都是静态文本。
这个片断的fastm Template DOM结构如下:
静态文本
动态块zipcodes --
| --- 静态文本
静态文本
2.fastm的ValueSet是DOM概念的又一次飞跃
Fastm模板DOM结构的一个核心特性就是,只能读取,不能改变。
PHP代码每次装载一块PHP模板,然后动态更换里面的变量部分的值。PHP模板从本质上讲是可以读取,也是可以操作改变的。
HTML(WML,XML)DOM更是如此。程序直接修改DOM节点的值,才能得到不同的动态结果。可以说,XML DOM天生就是用来操作改变的。XML DOM本身又是模板,又是数据。
可以改变的DOM结构不能够用在多线程的环境下。每个线程必须获取自己的新鲜DOM备份,进行操作改变,得到自己的动态结果。想想看,在一个静态文本占绝大部分的DOM结构里,这种做法将造成多么大的空间和时间上的浪费。
Fastm模板的DOM结构是只读的,不能改变。所以一个Fastm DOM可以用在多线程的环境中。
既然我们不能改动Fastm Template DOM,那么我们如何给Fastm Template DOM赋值呢?我们如何利用Fastm Template DOM获得动态结果呢?
Fastm引入了ValueSet的概念。ValueSet是一个树形结构的动态数据集,用来匹配只读的Fastm模板DOM结构,生成动态结果。
程序员必须事先构造好整个树形动态数据集(ValueSet DOM),然后把和ValueSet DOM和Fastm Template DOM结合起来,生成动态结果。
所以,fastm的整个使用如下:
(1)程序的整个运行过程中,fastm模板文件(也就是加了BEGIN-END注释的HTML文件)只需要被解析一次,生成一个Fastm Template DOM。
(fastm模板解析速度奇快,比JSP编译,Velocity解析,XML DOM解析,都快很多,大部分情况下甚至快于SAX解析。而且fastm DOM和原始fastm模板文件的大小几乎一样大,只多了一个List记录不同的块,空间效率也要高出)
(2)程序生成不同的ValueSet DOM,匹配只读的Fastm DOM,生成不同的动态结果。
(由于fastm Template DOM结构的简单高效,整个匹配过程很快。通常情况下,时间效率甚至高于最快的纯JSP或Servlet。ValueSet DOM的空间效率比不上纯JSP或Servlet,但经过合理重用,至少可以接近纯JSP或Servlet的空间效率。以后的高级应用话题系列会详细讲解这个问题。)
比如,我们来为上面的Template DOM结构(zipcode Select)构造一个ValueSet DOM。
String[] zipcodes = {“361005”, “100008”};
IValueSet top = new ValueSet(); // 对应上面的整个HTML片断
List items = new ArrayList(); // 对应 动态部分zipcodes
for(int i = 0; i
IValueSet item = new ValueSet();
item.setVariable(“”, zipcodes[i]);
items.add(item);
}
top.setDynamicValueSets(“zipcodes”, items);
我们把top这个ValueSet DOM和Template DOM结合起来。就生成如下结果。
我们可以看到,Template DOM节点和ValueSet DOM节点之间不是一一对应的关系,而是一对多的关系。一个Template DOM节点对应一个ValueSet List。ValueSet List包含多少个ValueSet,这个Template DOM节点就显示所少次。
比起TagLib来,fastm的优势显而易见。fastm的几行代码,或者一个方法,可以实现一个或几个TagLib的功能。比起任何其它的页面技术来说,其它页面技术能做到的,或者做的好的,fastm都能够做得到,而且做的更好。而fastm能做到很多其它页面技术做不到的事情。好了。不多说了。J
ValueSet DOM和Template DOM的分开,是一个极大的思路上的创新和飞跃。
毕竟,页面中的动态部分,和静态比起来,是非常小的一部分。ValueSet DOM代表动态部分,由程序随时生成,可以存在多份。Template DOM代表静态部分,只需要解析一次,而且只需要一份。
ValueSet DOM和Template DOM的分开,更是一种前所未有彻底的显示和数据的分离。比XML/XSLT的方法更加彻底。XML确实是纯粹的数据,但XSLT中却不可避免的要包含逻辑。ValueSet DOM是纯粹的数据,没有任何逻辑,Template DOM是纯粹的显示模板,也没有任何逻辑。
一份Template DOM可以用多个ValueSet DOM赋值。同样,一个ValueSet DOM也可以用于多个Template DOM,把相同的数据显示在不同风格的模板中。
比如,我们还有这样一个HTML片断:
我们把上面的top ValueSet赋给这个模板。得到的结果如下。
361005 |
100008 |
我们可以看到,Template DOM就是模板,只包含显示风格和分块定义。ValueSet DOM就是数据,只包含数据。
Fastm具有其它页面生成技术不可比拟的优越性:
所见即所得,模板与数据的彻底分离,模板与数据的多对多自由匹配,易学易用,开发速度快,运行空间小,运行速度快。
就我个人的感觉来说,fastm简直是解决一切页面技术问题的银弹。
上文可能有些“自卖自夸”之嫌,特解释如下:
我不是一个善于吹嘘的人。否则早就从事推销员、公关宣传之类有前途的职业去了。而且由于自我推销能力和社会关系学能力的欠缺,已经造成了自身的能力、精力和时间上的极大浪费。我痛苦自己的现状,却不后悔。我了解社会的游戏规则,但没有能力,也不愿、不屑参与。内心里总是存在着一种痴心妄想,希望创造一个游离于现有规则之外的奇迹。
另外,我是一个客观求实、头脑严密清晰的人,具有“外举不避仇,内举不避子”的负责的学术精神。有一分,当说一分,决不多说一分,也绝不少说一分。
3.fastm的可重用性的核心在于ValueSet DOM
JSP技术的可重用性的核心在于TagLib。
XML DOM的可重用性的核心在于DOM节点的通用操作。
Fastm的可重用性的核心在于ValueSet DOM节点的通用操作。比如,上面讲的那段生成ValueSet的代码。
Template DOM本身可以作为一个只读模板来使用,同样,Template DOM下面包含有的任何Template DOM结构也可以作为一个独立的只读模板来使用。这点和XML DOM一样。XML DOM的任何一个节点可以作为独立的节点来使用。
ValueSet DOM不仅是数据重用的核心,同样是模板拼装重用的中枢。fastm实现各模板之间各个块的搬运拼装,再容易不过了。实现所谓的Tile功能,小菜一碟。
JSP,还有某些表示逻辑的TagLib,Velocity模板,XSL文件,都是包含逻辑的模板。个人认为,模板中包含逻辑,是一种很可笑的行为。模板的长处在于表现页面布局、显示风格,而不在于逻辑。为什么不让擅长逻辑的Java去处理逻辑?
在fastm中,Template DOM和ValueSet DOM中都不含有任何逻辑。所有的逻辑都落在Java代码中。而Java是一种高度面向对象的语言,其结构性和重用性是任何模板语言不能比拟的。所以,fastm的可重用性的核心在于ValueSet DOM节点的通用操作。这些通用操作的代码当然由Java实现。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news on April 17, HMD teamed up with the well-known beer brand Heineken and the creative company Bodega to launch a unique flip phone - The Boring Phone. This phone is not only full of innovation in design, but also returns to nature in terms of functionality, aiming to lead people back to real interpersonal interactions and enjoy the pure time of drinking with friends. Boring mobile phone adopts a unique transparent flip design, showing a simple yet elegant aesthetic. It is equipped with a 2.8-inch QVGA display inside and a 1.77-inch display outside, providing users with a basic visual interaction experience. In terms of photography, although it is only equipped with a 30-megapixel camera, it is enough to handle simple daily tasks.

According to news on April 26, ZTE’s 5G portable Wi-Fi U50S is now officially on sale, starting at 899 yuan. In terms of appearance design, ZTE U50S Portable Wi-Fi is simple and stylish, easy to hold and pack. Its size is 159/73/18mm and is easy to carry, allowing you to enjoy 5G high-speed network anytime and anywhere, achieving an unimpeded mobile office and entertainment experience. ZTE 5G portable Wi-Fi U50S supports the advanced Wi-Fi 6 protocol with a peak rate of up to 1800Mbps. It relies on the Snapdragon X55 high-performance 5G platform to provide users with an extremely fast network experience. Not only does it support the 5G dual-mode SA+NSA network environment and Sub-6GHz frequency band, the measured network speed can even reach an astonishing 500Mbps, which is easily satisfactory.

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

According to news on April 3, Taipower’s upcoming M50 Mini tablet computer is a device with rich functions and powerful performance. This new 8-inch small tablet is equipped with an 8.7-inch IPS screen, providing users with an excellent visual experience. Its metal body design is not only beautiful but also enhances the durability of the device. In terms of performance, the M50Mini is equipped with the Unisoc T606 eight-core processor, which has two A75 cores and six A55 cores, ensuring a smooth and efficient running experience. At the same time, the tablet is also equipped with a 6GB+128GB storage solution and supports 8GB memory expansion, which meets users’ needs for storage and multi-tasking. In terms of battery life, M50Mini is equipped with a 5000mAh battery and supports Ty

At work, ppt is an office software often used by professionals. A complete ppt must have a good ending page. Different professional requirements give different ppt production characteristics. Regarding the production of the end page, how can we design it more attractively? Let’s take a look at how to design the end page of ppt! The design of the ppt end page can be adjusted in terms of text and animation, and you can choose a simple or dazzling style according to your needs. Next, we will focus on how to use innovative expression methods to create a ppt end page that meets the requirements. So let’s start today’s tutorial. 1. For the production of the end page, any text in the picture can be used. The important thing about the end page is that it means that my presentation is over. 2. In addition to these words,

Regarding PPT masking, many people must be unfamiliar with it. Most people do not understand it thoroughly when making PPT, but just make it up to make what they like. Therefore, many people do not know what PPT masking means, nor do they understand it. I know what this mask does, and I don’t even know that it can make the picture less monotonous. Friends who want to learn, come and learn, and add some PPT masks to your PPT pictures. Make it less monotonous. So, how to add a PPT mask? Please read below. 1. First we open PPT, select a blank picture, then right-click [Set Background Format] and select a solid color. 2. Click [Insert], word art, enter the word 3. Click [Insert], click [Shape]

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.
