Yii Framework integrates the Smarty template engine

Source: Internet
Author: User
Yii Framework integrates the Smarty template engine

As one of the most mature and used templates, Smarty is familiar to everyone. If you are not familiar with other students, refer to the Smarty tutorial. But even if it doesn't, it doesn't matter. I will try to explain it in detail here for everyone to understand.

You can first understand the usage of the assign (), include, and display () functions in smarty. In addition, you also need to know the if-else and foreach labels. That's enough!

Configure Smarty support

1Create a class with the file name:CSmarty. php?The content is as follows:

 Template_dir = SMARTY_VIEW_DIR; $ this-> compile_dir = SMARTY_VIEW_DIR. self: DIR_SEP. 'Template _ C'; $ this-> caching = true; $ this-> cache_dir = SMARTY_VIEW_DIR. self: DIR_SEP. 'cache'; $ this-> left_delimiter ='
 '; $ This-> cache_lifetime = 0; // -- initial global data $ this-> assign ('base _ url', 'http: // www.ttall.net '); $ this-> assign ('index _ url', 'http: // www.ttall.net/index.php');} function init (){}}

Put the above class in the file directory :? Protected/extensions/

2. configurationProtected/config/main. php
Add the following code to the file:

 Dirname (_ FILE __). DIRECTORY_SEPARATOR. '.. ', 'name' => 'yibaiit tutorial network -www.yiibai.com', // preloading 'log' component 'preload' => array ('log '), // autoloading model and component classes 'import' => array ('application. models. * ', 'application. components. * ', 'application. extensions. * ', 'application. extensions. smarty. sysplugins. * ',), // application components 'components' => array ('user' => array (// enable cookie-based authentication 'allowautologin' => true ,), 'smarty '=> array ('class' => 'application. extensions. CSmarty ',),

3. find the fileProtected/components/Controller. php, Add two methods:

/*** Smarty assign () method **/public function assign ($ key, $ value) {Yii: app ()-> smarty-> assign ($ key, $ value);}/*** Smarty display () method **/public function display ($ view) {Yii: app () -> smarty-> display ($ view );}

In this way, we can directly call the two Smarty methods in each controller.

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.