关于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

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
