Home  >  Article  >  Backend Development  >  Thoughts about templates, please come in if you are familiar with templates. _PHP Tutorial

Thoughts about templates, please come in if you are familiar with templates. _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:27:26725browse


Using PHP for project development, due to PHP's interpretation and execution, we often face conflicts between execution speed, versatility, and programming efficiency, and then how to choose. The early versions of PHP were process-oriented and famous for their fast execution speed, which was increasingly difficult to develop complex applications. With the release of 4.3 and later versions, we can see that PHP will develop in the object-oriented direction.
Use PHP When developing projects, due to PHP's interpretation and execution, we often face conflicts between execution speed, versatility, and programming efficiency, and then how to choose. The early versions of PHP were process-oriented and famous for their fast execution speed. They were increasingly unable to develop complex applications. With the release of version 4.3 and later, we can see that PHP will develop in the object-oriented direction, so everyone gradually became accustomed to writing A large number of base classes and extensive use of include_once make our development orderly and the program logic clearer. After we completed the development, we found that the execution efficiency was not as good as before. PHP spent too much overhead on parsing and file calling. Using more than ten or twenty includes on a page will bring down PHP. Our attention begins to shift from the database to the optimization of PHP programs. On the one hand, it is understandable to use object-oriented mode for large data drivers. On the other hand, PHP code The simplicity and efficiency of C-like languages ​​should be highlighted. So we are confused: How to ensure the execution efficiency of large applications? This is a question I've been thinking about for a long time.
Maybe the above topics involve too much. Now I just want to talk specifically about the problem of processing a large number of templates: for hundreds of pages, each page has dynamic data, and a small number of pages involve more variables. How to facilitate it? How to implement template variable substitution? Should I just use set_var() to replace it line by line?
The widespread adoption of the template mechanism has made the mixing of PHP code and HTML a thing of the past, solving the conflict between front-end output and back-end programs. The front-end is transparent to programmers, and the back-end is also transparent to editors. The advantages are self-evident. As a metaphor, the price we pay is just to agree on a set of variable names and simply replace them. However, there are hundreds of templates for hundreds of web pages, and each page may have dozens of variables, so this simple variable replacement work has become cumbersome. Taking PHPLIB's template as an example, it requires dozens of lines of set_var (), I began to imagine a VIEW class that would inherit template, define an array member to save a variable name table for replacement, provide setvalue() and output() methods, and assign values ​​through setvalue($key,$value). It was very Obviously $value is the data taken out from the database, and $key is the variable to be replaced. The difficulty is how to determine the value of $key (that is, the name in {} in HTML). For example, in the template file my.tpl, like Like this:


Your username is
Position is It should look like this in the PHP file ($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 penultimate line, I use an array to pass the list of variables to be replaced. It is normal for a page to have ten or twenty template variables. The question is whether it is convenient to set the array at once. (that is, the variable names to be replaced such as "tpname" and "tpposition"), otherwise you can only set_var line by line. I am still exploring, and everyone is welcome to actively exchange solutions in this regard .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531885.htmlTechArticleUsing PHP for project development, due to PHP’s interpretation and execution, we often face execution speed, versatility, and programming efficiency Conflicts arise, and then there is the question of how to choose. Early versions of PHP were...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn