Home >php教程 >PHP开发 >Summary of advanced PHP templates

Summary of advanced PHP templates

黄舟
黄舟Original
2016-12-14 11:35:531102browse

How to use PHP to write code quickly, templates seem to be the only choice. But should a PHPer ultimately stick to using templates, give up on templates, or use his own templates?

The following ideas belong to all PHPers:

“The most important thing about templates is that they suit your own project needs, not your own habits. For example, a corporate promotional website has a small number of visits and a small number of updates. It’s not big. It’s the same with anything, or even not needed. Writing directly in PHP+HTML is probably the fastest. There’s nothing wrong with that. But if it’s a news system or a content publishing system, it’s very necessary to use templates. This technology. Because they all have one thing in common, that is, there is a type of page that uses the same style of page design. Depending on the actual situation, such as the size of the visits, the urgency of the project, and the programmer's proficiency in template technology Degree, choose the template technology that is most suitable for the project.. This is the first choice. "[from leslee]

"I have been exposed to templates since I started learning PHP. I use the smarty template engine (officially supported, powerful), and I have been using it for a long time. The longer I go, the more I feel that templates are a good thing. As long as it involves page display, I will use templates to write programs. I think other PHPers will feel the same way because of template design. The original intention is to separate the code and the page and process them separately. After all, no one wants to write code and process the page at the same time. "

" Some people say that SMARTY is already very good, but some people say that SUPERTPL is better, and some people prefer SMARTPL. PHPLIB I think each template has its own advantages and disadvantages. The key depends on what your project needs and which one is best! "[from feifengxlq] "There is definitely nothing wrong with using templates. I used PHPLIB initially. After a while, it felt too simple. You had to write code to replace each template variable, and you had to write a line of code for each reference to a template (maybe PHPLIB has a batch processing function, I am too naive to know), so I gave up. Later I came into contact with smarty when I was using plog to build a website. I felt that smarty was too large and could indeed speed up the development progress, but many functions were not actually used. Therefore, I finally wrote my own template engine. The function that the template needs to implement is very simple, it is just a template. Variable substitution function, plus include other templates and foreach block operation functions. "

"I still use PHPLIB, but I will write some functions or classes to encapsulate some of the most commonly used templates based on my own experience and needs. Output processing. "[From seraph]" I use my own template, and I have to write template parsing code, which is tiring. I choose to use existing excellent templates. "I saw some very good ones on other forums. Great people don't use templates... maybe they're not that advanced... Personally, it's better to use templates... at least they can divide the work... and speed up development. Use your own templates? I don't think it's necessary... Use ready-made ones. Just add some practical functions that you want to learn based on the ready-made ones. "[From Phzzy] "I don't use template engines, but I also use templates, but it's different from yours. Because the template engine is too slow, it feels difficult to use. ”

“I use a third party, preferably open source. As for which one to use, it depends on personal preference. ” [from leeyupeng] “I still tend to use my own templates that are simple and practical. "[From Yuhi] "Due to performance considerations, I generally do not use universal templates. I will make some trade-offs based on the actual situation, which has certain limitations. "[From Ben] "Decide whether to use templates or not, and what templates to use, based on your needs. It never hurts to be flexible. "[From xlmo] "I personally like simple and fast templates. You can consider using ready-made templates. However, I usually optimize them based on the actual project and delete some functions that are not used. ”

“Using templates generally does not cause bottlenecks. As for the efficiency, it mainly affects the following points:

1. After the program and template are separated, one file becomes two or more. Then read the template. IO is also time-consuming
2. Parsing templates is relatively complicated, generally requiring N preg_match, preg_replace and other functions to process or replace
3. The loading and replacement of templates obviously requires more work than not using templates

So to improve efficiency, pay attention to the following points:

1. Choose a template parsing program that supports compilation and caching. This has the most obvious benefit on performance. It depends on how you apply it flexibly.
2. Try to keep the parsing program as simple as possible. What functions If you put them all in, it will be more convenient to use, but the performance will be worse.
3. When making HTML template pages, follow simple principles, such as making them according to the xhtml standard. Try not to split a page into many templates and then include them. ”

"If you use a template, I think it's better to write one yourself if you have the ability. This is also an exercise. But if you want to use it, I think Xiaoqiang is very good. You can edit it visually." [from jejwe] "Personally, I also think that template files can be edited visually. Safety is very important. You cannot require that the production of template pages must also have a certain foundation in program development. "[From seraph]" Detailed analysis of specific issues, whether to use templates, choose an already formed excellent template class, or write it yourself, everything is the same. Depending on the specific project development, you can’t choose which one to choose.” [From Deepseath] “I feel that the existing templates are not perfect enough, or they have few functions, or they are slow, so it is better to do it yourself.”

“Summary. , PHP templates can make your code context clearer and the structure more rational. However, the development of PHP templates will always be much slower than the rapid development of PHP applications. In this case, it will affect the development of PHP applications. For every PHPer, there is no PHP template that is most suitable and perfect for him. Because the so-called PHP template is a popular thing, it is not personal. Therefore, if you can have a clear understanding of PHP characteristics and applications. On the basis of fully understanding the advantages and disadvantages of templates and the principles of PHP application, PHPer can get the PHP template that suits his own development style. This is also one of the most important ideas of PHP. What PHP provides is only methods and approaches. It is not the final solution. Therefore, templates, like PHP applications and products, can be transformed into their own PHP templates. Because the ideas of the creator are placed in it, it is limited by various conditions, such as time and experience. I think it is very difficult to make your own PHP template. In fact, what you need is not to reconstruct a PHP template, but to choose a PHP template that is closest to you and modify it, because PHP needs to inherit and innovate. Of course, you need to make your own PHP. The template is implemented step by step, and the latest ideas and concepts can be incorporated in a timely manner. Individual PHPer can start from the details, starting from where they need it most and their own development habits. The PHPer team can work together to localize PHP templates. It is especially practical for companies. Even if you only modify a symbol in the PHP template, just a symbol can show that you have modified it. The most important thing is that the PHP template belongs to you. It is fixed and will grow with you and become a witness of your PHP journey.

PHP does not require you to be very proficient in syntax; PHP does not require you to develop numerous libraries yourself; PHP does not require you to worry about performance. , stability and other characteristics of the language itself. What PHP needs is your logic; what PHP needs is your creativity; what PHP needs is your ideas!

For more related content, please pay attention to the PHP Chinese website. (www.php.cn)

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