For more information about the template, see.

Source: Internet
Author: User


PHP is used for project development. Due to PHP interpretation and execution, we often face conflicts between execution speed and versatility and programming efficiency, and how to choose between them. Early PHP versions are process-oriented and are well known for their fast execution speed. They are very slow in developing complex applications. With the release of PHP 4.3 and later versions, we can see that PHP will develop towards object-oriented development,
PHP is used for project development. Due to PHP interpretation and execution, we often face conflicts between execution speed and versatility and programming efficiency, and how to choose between them. Early PHP versions are process-oriented and are well known for their fast execution speed. They are very slow in developing complex applications. With the release of PHP 4.3 and later versions, we can see that PHP will develop towards object-oriented development, as a result, we gradually get used to writing a large number of base classes and using include_once. This makes our development orderly and the Program Logic clearer. After the development is complete, we find that the execution efficiency is not as high as before, PHP spends too much on parsing and file calling. using dozens or even 20 include items on a page will drag PHP down. Our attention begins to shift from database to PHP program optimization, on the one hand, the object-oriented model for large data drivers is beyond reproach. On the other hand, PHP code should highlight the simplicity and efficiency of C-like languages. So we are confused: how can we ensure the execution efficiency of large applications? This is a long-standing question.
Maybe there are too many topics mentioned above. I just want to talk about a large number of template processing problems: for the size of hundreds of pages, each page has dynamic data, A small number of pages involve many variables. How can we easily replace template variables? Is it directly replaced with set_var () in one row?
The extensive use of the template mechanism makes PHP code and HTML mixed into a historical one. It solves conflicts between foreground output and background programs, and the foreground is transparent to programmers, similarly, the background is transparent to the beauty editor, and its superiority is self-evident. The price we pay is simply to agree on a set of variable names and simply replace them. However, hundreds of webpages have hundreds of templates, and each page may have dozens of variables. Therefore, the simple variable replacement work becomes cumbersome. Take the PHPLIB template as an example, dozens of rows of set_var () are required. I started to imagine a VIEW class that inherits the template, defines a variable table that is saved by several group members for replacement, and provides setvalue () and output () setvalue ($ key, $ value) is used to assign values. Obviously, $ value is the data retrieved from the database, and $ key is the variable to be replaced, the difficulty is how to determine the value of $ key (the name in {} in HTML). For example, in the template file my. tpl is like this:


Your username is
Job is In the PHP file, it should be like this ($ name and $ position are dynamic data ):...... $ Tp-> set_file ("filehandle", "my. tpl "); $ vararr = array (" tpname "=> $ name," tpposition => $ position); tp-> set_var ($ vararr ); tp-> pparse ("out", "filehandle"); in the second to last line, I use an array to pass the list of variables to be replaced. a page with 10 or 20 template variables is normal, the problem is whether it is convenient to set the array subscript (that is, the names of the variables to be replaced, such as "tpname" and "tpposition") at one time. Otherwise, only set_var can be set in one row, I am still exploring. You are welcome to discuss the solutions in this regard.

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.