Yii,ci,yaf Framework +smarty Template use method, Yafsmarty
This article describes the use of YII,CI,YAF framework +smarty templates. Share to everyone for your reference, as follows:
Recently toss the framework of performance tests, which need to test the performance of each template with smarty, so toss a bucket, and now summarize. The Kohana framework +smarty template has been written before and is no longer duplicated here.
First, Yii framework +smarty template
Yii is covered with the Viewrenderer component.
1.1, download the YII framework and unzip, download the Smarty framework and unzip, copy the Smarty/libs folder to the YII framework application/protected/vendors below, and rename Smarty.
1.2,yii configuration file main.php
' Components ' =>array (' viewrenderer ' = = Array ( ' class ' = ' = ' Batman.protected.extensions.SmartyViewRender ', //Here is a smarty supported property ' config ' = = Array ( ' left_delimiter ' = ' = ' {# ', ' right_delimiter ' = ' #} ', ' template_dir ' = App_dir. "/views/", ' config_dir ' = App_dir. "/views/conf/", ' debugging ' = False, ' compile_dir ' = ' d:/temp/runtime ', ))
Where Batman is the alias I have defined in index.php.
Yii::setpathofalias (' Batman ', DirName (__file__)); Yii::import ("batman.protected.vendors.*");d efine (' App_dir ', DirName (__file__). ' /protected/');
1.3, create a new 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 ($ke y {0} = ' _ ') {//Not setting semi-private properties $this->_smarty-> $key = $value; }}} yii::registerautoloader (' Smartyautoload '); The Public function RenderFile ($context, $file, $data, $return) {foreach ($data as $key + = $value) $this->_smart Y->assign ($key, $value); $return = $this->_smarty->fetch ($file); if ($return) return $return; else echo $return; }}
1.4, verify
Create a new 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 normal library, and when used, the controller code resembles the following:
Public Function Index () { $this->load->library (' smarty/ci_smarty ', ' ', ' smarty '); $this->smarty->assign ("title", "Congratulations on your Smarty installation success!") "); $this->smarty->assign ("Body", "Welcome to use Smarty template Engine"); $arr = Array (1=> ' Zhang ',2=> ' Xing ',3=> ' Wang '); $this->smarty->assign ("MyArray", $arr); $this->smarty->display (' index_2.html ');}
This method with CI comes with the method of using the template
Copy the Code code as follows: $this->load->view ();
Disharmony, and to a series of
Copy the Code code as follows: $this->smarty->assign ();
Statement, trouble not to say, also destroyed the original CI simple beauty, so resolute spit it.
How to maintain the simplicity of CI loading view, the answer is to overwrite the view () method of the loader class. Well, let's begin.
2.1, Conditions:
To the official website now the CI Framework and the smarty template.
2.2, make sure CI can run.
Extract the CI framework into the site and directory, first write a controller without the smarty template output "Hello World".
2.3, Introduction of Smarty
Will smarty decompression, libs folder to Application/third_paty below, and will libs rename smarty, rename take what all OK, here is called Smarty Bar.
2.4, covering the view () method of the loader class
Because the view () method is in the loader class, I want to overwrite the loader view () method.
Let's see how $this->load->view () works. There's a line in the constructor of the Ci_controller class.
Copy the code as follows: $this->load =& load_class (' Loader ', ' core ');
The Load_class function will first find Config_item (' Subclass_prefix ') under Application/core. loader.php file, can not find the system/core below to find loader.php. Config_item (' Subclass_prefix ') is the prefix written in the configuration file that you want to inherit from the subclass of the CI core class. I am using the default value ' My_ '. After the file is found, require the file, and then new My_loader (if application/core/my_loader.php exists), or new Loader, is assigned to $this->load.
Create a new 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. ' Views '. Ds;//smarty template file points to 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/cache '; $this->smarty->left_delimiter = ' {# '; $this->smarty->right_delimiter = ' #} '; Public Function View ($view, $vars = Array (), $return = FALSE) {//Check if view file exists $view. = Config_item (' tem Plates_ext '); $file = APPPATH. ' Views '. 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->assi GN ($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 the Template_ext as ". html", so it's OK. Let's check it out.
2.5, verify
Build a home.php underneath the controller.
Class Home extends Ci_controller {public Function index () { $data [' todo_list '] = Array ("Clean house", ' Call Mom ', ' Ru n Errands '); $data [' title '] = "Congratulations on your Smarty installation success!" "; $data [' body '] = "Welcome to Smarty Template Citation"; $arr = Array (1=> ' Zhang ',2=> ' Xing ',3=> ' Wang '); $data [' myarray '] = $arr; $this->load->view (' Index_2 ', $data); }}
Build a index_2.html under the views
Smarty Installation Test{# $title #}
{# $body #}
{#foreach from= $myarray item=v#}
- {# $v #}
{#/foreach#}
Well, you can try your work.
Third, yaf frame +smarty template
YAF is to use the bootstrap file bootstrap.php to load the smarty.
3.1, using bootstrap
In index.php
Copy the Code code as follows: $app->bootstrap ()->run ();
Introducing bootstrap.php Files
3.2, import the Smarty in the application/bootstrap.php file.
<?phpclass Bootstrap extends Yaf_bootstrap_abstract {public Function _initsmarty (Yaf_dispatcher $dispatcher) { $smarty = new Smarty_adapter (null, Yaf_application::app ()->getconfig ()->smarty); Yaf_dispatcher::getinstance ()->setview ($smarty); }}
3.3, add Smarty_adapter class
Unzip the Smarty and place it under the Application/library folder and rename it to Smarty. Create a new adapter.php under Smarty and make sure the Smarty.class.php is under smarty/libs/. adapter.php content:
<?phpyaf_loader::import ("smarty/libs/smarty.class.php"); Yaf_loader::import ("smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php"); Yaf_loader::import ("smarty/libs/sysplugins/smarty_internal_templatelexer.php"); Yaf_loader::import ("smarty/libs/sysplugins/smarty_internal_templateparser.php"); Yaf_loader::import ("smarty/libs/sysplugins/smarty_internal_compilebase.php"); Yaf_loader::import ("smarty/libs/sysplugins/smarty_internal_write_file.php"); class Smarty_adapter implements Yaf_ view_interface{/** * Smarty Object * @var Smarty * * Public $_smarty;/** * Constructor * * @param string $tmplPath * @param array $extraParams * @return void */Public function __construct ($tmplPath = null, $extraParams = Array ()) { $this->_smarty = new Smarty; if (null!== $tmplPath) {$this->setscriptpath ($tmplPath); } foreach ($extraParams as $key = + $value) {$this->_smarty-> $key = $value; }}/** * Return the template Engine Object * * @return Smarty * *Public Function Getengine () {return $this->_smarty;}/** * Set the path to the templates * * @param string $path The directory to set as the path. * @return void */Public Function Setscriptpath ($path) {if (is_readable ($path)) {$this->_smarty->template_dir = $path; Return } throw new Exception (' Invalid path provided '); }/** * Retrieve the current template directory * * @return String */Public Function Getscriptpath () {return $this- >_smarty->template_dir; }/** * Alias for Setscriptpath * * @param string $path * @param string $prefix Unused * @return void */public func tion Setbasepath ($path, $prefix = ' Zend_view ') {return $this->setscriptpath ($path);}/** * Alias for Setscriptpath * * @param string $path * @param string $prefix Unused * @return void */Public Function Addbasepath ($path, $prefix = ' Zend_view ') {return $this->setscriptpath ($path),}/** * Assign A variable to the template * * @param string $ke Y The VariabLe name. * @param mixed $val the variable value. * @return void */Public Function __set ($key, $val) {$this->_smarty->assign ($key, $val);}/** * allows testing with empty () and isset () to Work * * @param string $key * @return Boolean */Public function __isset ($key) {return ( Null!== $this->_smarty->get_template_vars ($key)); }/** * allows unset () on object properties to work * * @param string $key * @return void */Public Function __unset ( $key) {$this->_smarty->clear_assign ($key);}/** * Assign variables to the template * * allows setting a SPECIF IC key to the specified value, OR passing * An array of key = value pairs to set en masse. * * @see __set () * @param string|array $spec The assignment strategy to use (key or * array of key = value pairs) * @param mixed $value (Optional) If Assigning a named variable, * Use this as the value. * @return void */Public function assign ($spec, $value = null) {if (Is_array ($spec)) {$tHis->_smarty->assign ($SPEC); Return } $this->_smarty->assign ($spec, $value); }/** * Clear all assigned variables * * Clears all variables assigned to Zend_view either via * {@link assign ()} or P Roperty overloading * ({@link __get ()}/{@link __set ()}). * * @return void */Public Function Clearvars () {$this->_smarty->clear_all_assign ();}/** * Processes a Templa Te and returns the output. * * @param string $name the template to process. * @return string the output. */Public function render ($name, $value = NULL) {return $this->_smarty->fetch ($name);} public function display ($n Ame, $value = NULL) {echo $this->_smarty->fetch ($name);}}
3.4,smarty configuration file.
Take a look at our Conf/application.ini file.
[common]application.directory = App_path "/application" application.dispatcher.catchException = trueapplication.view.ext= "TPL" [smarty:common];configures for smartysmarty.left_delimiter = ' {# ' Smarty.right_ delimiter = "#}" Smarty.template_dir = App_path "/application/views/" smarty.compile_dir = '/data1/www/cache/ ' Smarty.cache_dir = '/data1/www/cache/' [Product:smarty]
3.5, verify
Create a new controller and add a method:
Public Function twoaction () { $this->getview ()->assign (' content ', ' Hello World ');}
Create a new template Two.tpl
A Smarty Adapter Example{# $content #}
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- PHP based on the method of using Smarty template in Yii framework
- Using Smarty3 basic configuration in CodeIgniter
- How to use the Smarty Advanced feature Object
- PHP implementation of Smarty template infinite Pole classification method
- Smarty Simple Application Example
- Summary of examples of common methods in Smarty
- Thinkphp+smarty+uploadify for no-refresh uploads
http://www.bkjia.com/PHPjc/1086649.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086649.html techarticle YII,CI,YAF Framework +smarty Template use method, Yafsmarty This article describes the YII,CI,YAF framework +smarty template usage. Share to everyone for your reference, as follows: Recently ...