Details on how to make ThinkPHP template engine achieve the best efficiency, thinkphp Template

Source: Internet
Author: User

Details on how to make ThinkPHP template engine achieve the best efficiency, thinkphp Template

This article analyzes how to make ThinkPHP's template engine achieve the best efficiency. We will share this with you for your reference. The details are as follows:

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 documentation,This default built-in template engine is highly efficient, but not optimal. 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:

<?phpreturn array( ... 'TMPL_ENGINE_TYPE' =>'PHP', ...);?>

Then, 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

<?phpclass IndexAction extends Action { public function index(){  $this->display(); }}

Index.html template code:

<Html> 

Output:

$ Title = 'wangzhi blog'; echo $ title;

Put "<? Php?> "Changed to <php> </php>, the variable cannot be interpreted, indicating that the <php> </php> label is not supported.

2. directly use the query statement on the template

The Controller code is the same as 1. The template code is as follows:

<Html> 

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!

3. Call the query results allocated by the Controller on the template.

IndexAction. class. php controller code

<?php class IndexAction extends Action { public function index(){  $vo=M('News')->find();  $this->assign('vo', $vo);  $this->display(); }}

Template index.html code

<Html> 

Output:

Welcome to WBlog

This is similar to the template engine used by the system by default.

4. 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:

<Html> 

Output:

af10ef457ed637b91955369297b8e640

Instead of the tag syntax of the system default template engine (relatively speaking), function calls are so simple!

Summary: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, which is quite easy to write, however, the template tag of the default template engine is ineffective.

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.