您好,欢迎访问一九零五行业门户网

yii,CI,yaf框架+smarty模板使用方法_php实例

本文实例讲述了yii,ci,yaf框架+smarty模板使用方法。分享给大家供大家参考,具体如下:
最近折腾了框架的性能测试,其中需要测试各个模板跟smarty配合的性能,所以折腾了一桶,现总结一下。之前已经写过kohana框架+smarty模板,这里不再重复了。
一、yii框架+smarty模板
yii是覆盖了viewrenderer组件。
1.1,下载yii框架并解压,下载smarty框架并解压,将smarty/libs文件夹拷到yii框架application/protected/vendors下面,并重命名smarty。
1.2,yii配置文件main.php
'components'=>array( 'viewrenderer' => array( 'class'=>'batman.protected.extensions.smartyviewrender', // 这里为smarty支持的属性 'config' => array ( 'left_delimiter' => {#, 'right_delimiter' => #}, 'template_dir' => app_dir . /views/, 'config_dir' => app_dir . /views/conf/, 'debugging' => false, 'compile_dir' => 'd:/temp/runtime', ))
其中batman是我已经在index.php定义好的别名。
yii::setpathofalias('batman', dirname(__file__));yii::import(batman.protected.vendors.*);define('app_dir', dirname(__file__).'/protected/');
1.3,在protected/extensions/下面新建smartyviewrender.php
_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,验证
新建一个hellocontroller.php
render('world', array('content'=>'hello world')); }}
新建一个word.html
{#$content#}
二、ci框架+smarty模板
网上很多方法,将smarty作为一个普通的library,在使用的时候,controller代码类似于下面:
public function index(){ $this->load->library('smarty/ci_smarty', '', 'smarty'); $this->smarty->assign(title,恭喜你smarty安装成功!); $this->smarty->assign(body,欢迎使用smarty模板引擎); $arr = array(1=>'zhang',2=>'xing',3=>'wang'); $this->smarty->assign(myarray,$arr); $this->smarty->display('index_2.html');}
这种方法跟ci自带的使用模板的方法
复制代码 代码如下:
$this->load->view();
不和谐,而且要一系列的
复制代码 代码如下:
$this->smarty->assign();
语句,麻烦不说,还破坏了原本ci的简洁美,所以果断唾弃之。
那怎么保持ci加载view时的简洁美呢,答案就是覆盖loader类的view()方法。好吧,let's begin。
2.1,条件:
到官网上现在ci框架和smarty模板。
2.2,确保ci已经能跑起来
将ci框架解压到网站跟目录下,先写一个不带smarty模板的controller输出“hello world”。
2.3,引入smarty
将smarty解压,将libs文件夹考到application/third_paty下面,并将libs重命名smarty,重命名取什么都ok了,这里就叫smarty吧。
2.4,覆盖loader类的view()方法
因为view()方法在loader类里,所以我要覆盖loader的view()方法。
先看看$this->load->view()是怎么工作的?ci_controller类的构造函数里有这么一行
复制代码 代码如下:
$this->load =& load_class('loader', 'core');
load_class函数会先在application/core下面找config_item('subclass_prefix').loader.php文件,找不到再到system/core下面找loader.php。config_item('subclass_prefix')就是在配置文件里写的你要继承ci核心类的子类的前缀。我使用的是默认值'my_'。找到文件后,require该文件,然后new my_loader(如果application/core/my_loader.php存在),或者是new loader,赋值给$this->load。
在application/core下面新建一个my_loader.php文件
smarty = new smarty (); // smarty 配置 $this->smarty->template_dir= apppath.'views'.ds;//smarty模板文件指向ci的views文件夹 $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('templates_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->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; } }}
我把template_ext配置成了.html,这样就ok了。我们来验证一下吧。
2.5,验证
在controller下面建一个home.php
class home extends ci_controller { public function index() { $data['todo_list'] = array('clean house', 'call mom', 'run errands'); $data['title'] = 恭喜你smarty安装成功!; $data['body'] = 欢迎使用smarty模板引; $arr = array(1=>'zhang',2=>'xing',3=>'wang'); $data['myarray'] = $arr; $this->load->view('index_2', $data); }}
在views下面建一个index_2.html
smarty安装测试{#$title#}{#$body#}
{#foreach from=$myarray item=v#} {#$v#} {#/foreach#}
好了,可以试试你的成果了。
三、yaf框架+smarty模板
yaf是利用引导文件bootstrap.php来加载smarty。
3.1,使用bootstrap
在index.php中用
复制代码 代码如下:
$app->bootstrap()->run();
引入bootstrap.php文件
3.2,在application/bootstrap.php文件中导入smarty。
getconfig()->smarty); yaf_dispatcher::getinstance()->setview($smarty); }}
3.3,添加smarty_adapter类
将smarty解压后放到application/library文件夹下,重命名为smarty。在smarty下新建adapter.php,确保smarty.class.php在smarty/libs/下。adapter.php内容:
_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 function 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 $key 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 specific 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 property overloading * ({@link __get()}/{@link __set()}). * * @return void */ public function clearvars() { $this->_smarty->clear_all_assign(); } /** * processes a template 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($name, $value = null) { echo $this->_smarty->fetch($name); }}
3.4,smarty配置文件。
再来看看我们的conf/application.ini文件
[common]application.directory = app_path /applicationapplication.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,验证
新建一个controller,添加方法:
public function twoaction() { $this->getview()->assign('content', 'hello world');}
新建一个模板two.tpl
a smarty adapter example{#$content#}
希望本文所述对大家php程序设计有所帮助。
其它类似信息

推荐信息