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.