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

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











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
