关于template的思考,对模板熟悉者请进。
用PHP进行项目开发,由于PHP的解释执行,我们经常会面临执行速度和通用性、编程效率产生矛盾,然后又如何取舍的问题。PHP的早期版本都是面向过程的,以执行速度快著称,在开发复杂应用时日显捉襟见肘,随着4.3及以后版本的发行可以看到PHP将向面向对象方向发展,
用PHP进行项目开发,由于PHP的解释执行,我们经常会面临执行速度和通用性、编程效率产生矛盾,然后又如何取舍的问题。PHP的早期版本都是面向过程的,以执行速度快著称,在开发复杂应用时日显捉襟见肘,随着4.3及以后版本的发行可以看到PHP将向面向对象方向发展,于是大家逐渐习惯写大量的基类,大量的使用include_once,这样使我们的开发变得有序,程序逻辑更清晰,当我们开发完成之后,发现执行效率不如以前,PHP在解析和文件调用上花费过多的开销,一个页面使用十几二十个include会将PHP拖垮,我们的注意力开始从数据库转向PHP程序的优化,从一方面来讲,大型数据驱动用面向对象模式无可非议,另一方面,PHP的代码应该突出类C语言的简洁高效。于是我们迷茫了:如何保证大型应用的执行效率?这是我思考已久的问题。
也许上面的话题涉及的太多,下面我只想具体谈谈大量模板处理的问题:对于几百个页面的规模,每页都有动态数据,少量页面涉及较多的变量,如何方便地实行模板变量替换?难道直接用set_var()一行一行地替换?
模板机制的广泛采用使PHP代码和HTML混杂在一起成为历史,解决了前台输出和后台程序的冲突,前台对程序员是透明的,同样后台对美编也是透明的,优越性不言而喻,而我们所付出的代价只是约定好一组变量名,并简单的替换一下而已。然而有几百个网页就有几百个模板,每一个页面可能有数十个变量,于是这一简单的变量替换工作也变成了累赘,以PHPLIB的template为例,就需要几十行set_var(),我开始设想一个VIEW类,它将继承template,定义一个数组成员保存用来替换的变量名表,提供setvalue()和output()方法,通过setvalue($key,$value)来赋值,很明显$value就是从数据库取出的数据,$key是将要替换的变量,难点就是如何确定$key(即HTML中{}中的名字)的值, 举个例子,比如在模板文件my.tpl中象这样:
您的用户名是
职位是 在PHP文件中应该象这样($name和$position是动态数据): …… $tp->set_file("filehandle","my.tpl"); $vararr=array("tpname"=>$name,"tpposition=>$position); tp->set_var($vararr); tp->pparse("out","filehandle"); 在倒数第二行我用数组传递要替换的变量列表,一个页面有一二十个模板变量是正常,问题是能不能方便地一次性设置好数组下标(即要替换的变量名例如"tpname"和"tpposition"),否则只能一行行地set_var,目前我仍在探索中,欢迎大家踊跃交流这方面的解决办法。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
