By default, the ThinkPHP framework system uses the built-in template engine by default. The built-in template engine supports mixed use of php original code and template labels in template files. By default, the ThinkPHP framework system uses the built-in template engine by default. The built-in template engine supports mixed use of php original code and template labels in template files.
According to ThinkPHP official development documents, the performance of this default built-in template engine is efficient, but it is not the best. To achieve the optimal efficiency of the template engine, you must use PHP as the template engine.
Using PHP as the template engine is actually very simple. you only need to configure it in the configuration file Conf/config. php of the project:
'Tmpl _ ENGINE_TYPE '=> 'php'
Using PHP as the template engine means that you cannot use the template tag of the default template engine on the template file. you can only use the original php code.
The following example demonstrates how to operate the PHP code on the template after using php itself as the template engine.
Download and install the wblog3.1.2 _ 3 blog program (you can also build your own project)
First, configure the W3note \ Conf \ config. php file to add a configuration item:
-
- Return array (
- ...
- 'Tmpl _ ENGINE_TYPE '=> 'php ',
- ...
- );
- ?>
Copy the code and clear the controller \ W3note \ Lib \ Action \ IndexAction. class. php and the code of the corresponding template \ W3note \ Tpl \ Index \ index.html for different debugging purposes.
Now that the basic work is complete, the debugging record is as follows:
1. use the original php code on the template
IndexAction. class. php controller code
-
- Class IndexAction extends Action {
- Public function index (){
- $ This-> display ();
- }
- }
Copy code index.html template code:
-
- Use original php code
-
- $ Title = 'wangzhi blog ';
- Echo $ title; // output variable
- ?>
Copy code output: Wangzhi blog
Like the original php code, variables can be declared on the template and variables can be output, and code comments can be identified. However, you must use the php start flag" No, why? Next you will know.
2. the controller code is the same as above. the template code is as follows:
-
-
- Use original php code
-
- $ Title = 'wangzhi blog ';
- Echo $ title;
Copy the code output: $ title = 'blog blog '; echo $ title;
In the template" "Replace The result does not support variable interpretation. Label.
3. directly use the query statement on the template
The controller code is the same as 1. the template code is as follows:
-
- Use original php code
-
- $ Vo = M ('news')-> find ();
- Echo $ vo ['title'];
- ?>
Copy code output: Welcome to WBlog
It seems that the controller has nothing to do while staying. The template can be written in this way, which is too flexible!
4. call the query results allocated by the controller on the template.
IndexAction. class. php controller code
-
- Class IndexAction extends Action {
- Public function index (){
- $ Vo = M ('news')-> find ();
-
- $ This-> assign ('vo', $ vo );
-
- $ This-> display ();
- }
- }
Copy code template index.html code
-
- Use original php code
-
- Echo $ vo ['title'];
- ?>
Copy code output: Welcome to WBlog
This is similar to the template engine used by the system by default.
5. call the function of the project function library on the template
The controller code is the same as 1. the template code is as follows:
-
- Use original php code
-
- Echo pwdHash ('ABC'); // call the encryption function pwdHash () of the project \ W3note \ Common \ common. php function library ()
- ?>
Copy the code output: af10ef457ed637b9195541797b8e640
Instead of the tag syntax of the system default template engine (relatively speaking), function calls are so simple!
Conclusion: using PHP as the template engine in ThinkPHP can optimize the performance of the template engine. in the template, you need to use the original php syntax, however, the template tag of the default template engine is ineffective.
Address: http://w3note.com/web/106.html