Home > Article > Backend Development > Thoughts about templates, please come in if you are familiar with templates. _PHP Tutorial
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: