Why does ThinkPHP have something similar to the template engine in PHP? So is smarty? How can this be used?

Source: Internet
Author: User
I have never found out where they are used? Separated the front-end and PHP dependencies? The HTML file does not write the PHP code, but there is the template engine code of smarty or Thinkphp in it. PHP programmers should learn it and front-end personnel should also learn it, does this increase the development time and difficulty? Thankless? In addition, the template engine is bound to be used at the PHP layer for parsing and replacing HTML code strings. We all know that string replacement is performance-consuming and time-consuming and performance-wasting. I think it is not cost-effective just to separate the frontend from PHP. I did not find out where they are used? Separated the front-end and PHP dependencies? The HTML file does not write the PHP code, but there is the template engine code of smarty or Thinkphp in it. PHP programmers should learn it and front-end personnel should also learn it, does this increase the development time and difficulty? Thankless? In addition, the template engine is bound to be used at the PHP layer for parsing and replacing HTML code strings. We all know that string replacement is performance-consuming and time-consuming and performance-wasting. I think it is not cost-effective just to separate the frontend from PHP. Is there any difference in my understanding of its role (just to separate the front-end and PHP for different roles? Please kindly advise! Reply content: the project is small. How can we quickly construct MVC and decouple page data when a person is tossing and tossing? So it is useless;
As the project grows and there are more people, the cost of code reuse, planning, coding specifications, and personnel communication has increased dramatically.
Rules are required to connect everyone's work;
To reduce the number of constraints, everyone is responsible for their own work, to avoid mutual interference, in order to improve efficiency.

Back to your question. I break it down into the following four points:
1. Why does the template engine appear?
2. Can PHP directly embed logic scripts in the HTML structure?
3. Why have they evolved into independent syntaxes?
4. How to consider performance overhead

1. Why does the template engine appear?
Z: Imagine two people working on a project, one for the page and one for the background data. (Frontend and backend)
Then they should focus on their own responsibilities.
For example, a person on a page should only care about how data is presented, how data is interacted, and how user data is transmitted to the background. Therefore, the current practical experience in the industry is that the front-end and backend can be independently developed by setting page parameters and interface protocols.

2. Can PHP directly embed logic scripts in the HTML structure?
Z: As mentioned above, we should focus on our own work in cooperation to avoid mutual interference. On the page, you may directly operate global variables, attributes, and methods in the PHP script in the page template. On the background, you may accidentally modify the parameters after passing values to the page... These are because their environments are not isolated.
A better solution is to provide an independent parameter scope for the Page Template and pass the page parameters separately.

3. Why have they evolved into independent syntaxes?
To reduce the Learning Burden on page development and make the page more intuitive. Please compare:

{{ innerHTML }}

Using MVC, I also think that using native PHP code is more efficient than the template engine at the View layer. This is irrelevant to the project size. PHP itself is the best template language. These template systems were previously left behind for code and style separation. Currently, the MVC Architecture is widely used, so it is useless.

PS: small systems should also use MVC. Machines are much cheaper than humans, and development efficiency is the most important. Generally, this type of question is caused by a small amount of code.
Separation is to facilitate maintenance and sacrifice some development efficiency to facilitate future maintenance.
If it is you, are you willing to maintain a messy system where PHP and HTML are all written together, or are you willing to maintain a three-layer MVC project?
Learning is not a big problem. Most frameworks are easy to get started. In addition, for the general framework, one of its primary problems is rapid development, and development efficiency is not a problem.
It is normal to run at a loss of efficiency, because the mvc mode is itself a solution to improve stability at the expense of efficiency. It has been three years of development with ThinkPHP! Simply put, feel
(1) first year: Because I am too lazy to learn the ThinkPHP built-in template engine syntax, I will directly use my familiar Smarty;
(2) The second year: because many ThinkPHP source codes are collected at ordinary times, the built-in ThinkPHP template syntax is used. If there is no way, I will read the built-in ThinkPHP engine again, it is actually a matter of two days. Basically, it is not much different from Smarty, so you can master it with just a few instances;
(3) The third year: When I was working on a project, I needed to use the native PHP string function in the template for a simple replacement. The result suddenly found that I would not write it, check the manual!
It's terrible. It was hard to imagine two years ago, and it was never wrong to close your eyes.
Therefore, various template engines make it easier for programmers to understand the native PHP language.
So now, before getting the project started, I will directly go to the public configuration file under the Application

'Tmpl _ ENGINE_TYPE '=> 'php', // native php 'tmpl _ TEMPLATE_SUFFIX' => '. php' for the Think template engine, // default template file suffix
Code and style separation, of course, now a trend is direct js, php do not understand, just know that I developed the project in the previous month, now two weeks or a week to deal. The framework is mainly for the services of our migrant workers, or to write code honestly. You can write a framework for your own purposes.

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.