Yii, CI, yaf framework + smarty template usage _ php instance-php Tutorial

Source: Internet
Author: User
Tags smarty template
This article describes how to use the yii, CI, yaf framework + smarty template, and how to use the yii, CI, and yaf framework to integrate the smaryt template, it has some reference value. if you need it, you can refer to the example below to describe how to use yii, CI, yaf framework + smarty template. We will share this with you for your reference. The details are as follows:

I recently tried to test the performance of the framework. I need to test the performance of each template in combination with smarty. so I tried a bucket and I will summarize it. I have already written the kohana framework + smarty template. I will not repeat it here.

I. yii Framework + smarty Template

Yii overwrites the viewRenderer component.

1.1 Download and decompress the yii Framework, download and decompress the smarty framework, copy the smarty/libs folder to the yii Framework application/protected/vendors folder, and rename the smarty.

1.2, yii configuration file main. php

'Components' => array ('viewrenderer' => array ('class' => 'Batman. protected. extensions. smartyViewRender ', // Here is the property 'config' => array ('left _ delimiter' => "{#", 'Right _ delimiter '=> "#}", 'Template _ dir' => APP_DIR. "/views/", 'config _ dir' => APP_DIR. "/views/conf/", 'destgging' => false, 'compile _ dir' => 'd:/temp/runtime ',))

Here, batman is an alias that I have defined in index. php.

Yii::setPathOfAlias('batman', dirname(__FILE__));Yii::import("batman.protected.vendors.*");define('APP_DIR', dirname(__FILE__).'/protected/');

1.3. create SmartyViewRender. php under protected/extensions /.

<?phpclass SmartyViewRender extends CApplicationComponent implements IViewRenderer { public $fileExtension = '.html'; private $_smarty = null; public $config = array(); public function init() {  $smartyPath = Yii::getPathOfAlias('batman.protected.vendors.smarty');  Yii::$classMap['Smarty'] = $smartyPath . '/Smarty.class.php';  Yii::$classMap['Smarty_Internal_Data'] = $smartyPath . '/sysplugins/smarty_internal_data.php';  $this->_smarty = new Smarty();  // configure smarty  if (is_array ( $this->config )) {   foreach ( $this->config as $key => $value ) {    if ($key {0} != '_') { // not setting semi-private properties     $this->_smarty->$key = $value;    }   }  }  Yii::registerAutoloader('smartyAutoload'); } public function renderFile($context, $file, $data, $return) {   foreach ($data as $key => $value)    $this->_smarty->assign($key, $value);  $return = $this->_smarty->fetch($file);  if ($return)    return $return;  else    echo $return; }}

1.4, verification

Create a HelloController. php

<?phpclass HelloController extends Controller { public function actionWorld() {  $this->render('world', array('content'=>'hello world')); }}

Create a new word.html

{#$content#}

II. CI framework + smarty Template

There are many ways to use smarty as a common library on the Internet. when using it, the controller code is similar to the following:

Public function index () {$ this-> load-> library ('smarty/Ci_smarty ', '', 'smarty '); $ this-> smarty-> assign ("title", "Congratulations, smarty has been installed successfully! "); $ This-> smarty-> assign (" body "," Welcome to the smarty template engine "); $ arr = array (1 => 'zhang ', 2 => 'X', 3 => 'wang'); $ this-> smarty-> assign ("myarray", $ arr ); $ this-> smarty-> display('index_2.html ');}

This method is similar to the template used by CI.

The code is as follows:

$ This-> load-> view ();


Not harmonious, but also requires a series

The code is as follows:

$ This-> smarty-> assign ();


Statement, not to mention, but also damage the conciseness and beauty of the original CI, so decisive abandon.

So how can we keep the CI simple and beautiful when loading the view? the answer is to overwrite the view () method of the Loader class. Let's get started.

2.1, condition:

The CI framework and smarty template are now available on the official website.

2.2 to ensure that the CI can run

Extract the CI framework to the website and directory, and first write a controller without the smarty template to output "hello world ".

2.3, introducing smarty

Decompress the smarty, test the libs folder under application/third_paty, rename libs to smarty, and rename everything. this is called smarty.

2.4. overwrite the view () method of the loader class.

Because the view () method is in the Loader class, I need to overwrite the view () method of the Loader.

First, let's see how $ this-> load-> view () works? The constructor of the CI_Controller class has such a row.

The code is as follows:

$ This-> load = & load_class ('loader ', 'core ');


The load_class function will first find the config_item ('subclass _ prefix'). Loader. php file under application/core, and then find Loader. php under system/core. Config_item ('subclass _ prefix') is the prefix of the subclass of the CI core class written in the configuration file. The default value is 'My _'. Find the file, require the file, and then assign the value to $ this-> load for new MY_Loader (if application/core/MY_Loader.php exists) or new Loader.

Create a MY_Loader.php file under application/core.

<? Phpdefine ('Ds', DIRECTORY_SEPARATOR); class MY_Loader extends CI_Loader {public $ smarty; public function _ construct () {parent :__ construct (); require APPPATH. 'third _ party '. DS. 'smarty '. DS. 'smarty. class. php '; $ this-> smarty = new Smarty (); // smarty configuration $ this-> smarty-> template_dir = APPPATH. 'view '. DS; // The smarty template file points to the ci's views folder $ this-> smarty-> compile_dir = 'd:/temp/tpl_c/'; $ this-> smarty-> config_dir = APPPATH. 'libraries/smarty/configs/'; $ this-> smarty-> cache_dir = 'd:/temp/caccache '; $ this-> smarty-> left_delimiter = '{#'; $ this-> smarty-> right_delimiter =' #}';} public function view ($ view, $ vars = array (), $ return = FALSE) {// check if view file exists $ view. = config_item ('Templates _ ext '); $ file = APPPATH. 'view '. DS. $ view; if (! File_exists ($ file) | realpath ($ file) === false) {exit (_ FILE _. ''. _ LINE __."
View file {$ file} does not exist,
{$ File }=>{$ view} ");} // changed by simeng in order to use smarty debug foreach ($ vars as $ key => $ value) {$ this-> smarty-> assign ($ key, $ value);} // render or return if ($ return) {ob_start ();} $ this-> smarty-> display ($ view); if ($ return) {$ res = ob_get_contents (); ob_end_clean (); return $ res ;}}}

I configured template_ext to ". html", so that it is OK. Let's verify it.

2.5, verification

Create a home. php file under the controller.

Class Home extends CI_Controller {public function index () {$ data ['Todo _ list'] = array ('Clean House', 'Call MS', 'Run errands '); $ data ['title'] = "Congratulations, smarty has been installed successfully! "; $ Data ['body'] =" Welcome to the smarty template Introduction "; $ arr = array (1 => 'zhang', 2 => 'X ', 3 => 'Wang '); $ data ['myarray'] = $ arr; $ this-> load-> view ('index _ 2', $ data );}}

Create an index_2.html File Under views

 
  

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.