yaf 整合自定义 视图引擎(smarty)
来源:互联网 发布:淘宝拍机器注意事项 编辑:程序博客网 时间:2024/05/29 17:19
参考:
http://www.oschina.net/question/812776_71817
http://yaf.laruence.com/manual/yaf.class.dispatcher.setView.html
这两者都是在bootstrap.php中写_initSmarty()函数来重新实现yaf的视图接口
这中方案默认的是存放模版文件的上级文件夹名字必须是views
根据项目要求,要求可以自定义存放模版文件的文件夹名字,过程及注意事项记录如下:
1.没有在bootstrap.php中写_initSmarty()函数来重新实现yaf的视图接口,而是在路由结束时的hook代码中来重写视图接口
1 <?php 2 class LayoutPlugin extends Yaf_Plugin_Abstract { 3 4 public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { 5 echo "Plugin routerStartup called <br/>\n"; 6 } 7 8 public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { 9 echo "Plugin routerShutdown called <br/>\n";10 //整合smarty11 $dispatcher = Yaf_Dispatcher::getInstance();12 // $dispatcher->disableView();13 $dispatcher->autoRender(false);14 $objSmarty = new Smarty_Adapter;15 $dispatcher->setView($objSmarty);16 17 }18 19 public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {20 echo "Plugin DispatchLoopStartup called <br/>\n";21 }22 23 public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {24 echo "Plugin PreDispatch called <br/>\n";25 }26 27 public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {28 echo "Plugin postDispatch called <br/>\n";29 }30 31 public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {32 echo "Plugin DispatchLoopShutdown called <br/>\n";33 }34 }
此时可以确保yaf已经分析URL完毕,确定了moduleName,controllerName,ActionName,
此时重写yaf视图接口就可以根据不同的moduleName去设定不同的smarty模版目录
注意几点:
①在bootstrap.php中写_initSmarty()函数来重新实现yaf的视图接口:
1 //smarty2 public function _initSmarty(Yaf_Dispatcher $dispatcher)3 {4 var_dump('@@@@@@@@@');5 }
结果:
1 string '@@@@@@@@@' (length=9)2 3 Plugin routerStartup called4 Plugin routerShutdown called5 Plugin DispatchLoopStartup called6 Plugin PreDispatch called7 Plugin postDispatch called8 Plugin DispatchLoopShutdown called
说明:在分析URL得到 moduleName,controllerName,ActionName 之前已经执行了_initSmarty();如果在这里边重写接口,灵活性就不是很大
② 关闭yaf自动默认的display动作(hook代码的第13行:$dispatcher->autoRender(false);)
否则yaf在执行完action内的代码后会根据当前的controllerName和actionName去尝试加载controllerName/actionName.html,如果加载不到就会抛出异常:
e.g. 访问本地:www.yaftwo.com/test/index/aaa时,打开自动默认的display动作
1 Plugin routerStartup called2 Plugin routerShutdown called3 Plugin DispatchLoopStartup called4 Plugin PreDispatch called5 -------smarty say hello to you !-------6 7 object(SmartyException)[24]8 protected 'message' => string 'Unable to load template file 'F:/zzbserv/apache/htdocs/yaftwo/code/application/modules/test/templates/index\aaa.html'' (length=117)9 ...
说明:第5行的输出说明已经成功display,但是紧接着就抛出了异常,内容很明显是因为找不到默认的disply路径
2.重写yaf视图接口
注意几点:
① setScriptPath()接口函数内不要写判断模版路径是否存在的代码,因为除了在重写视图接口时会执行这行代码外,yaf还会在"分发请求前"默默的执行这个函数,并且传递给它的实参是yaf默认的视图存放路径:views/actionName,此时如果你的layout中没有这个路径的话,你的程序就会报错
帖代码看看这个成员函数什么时候会被执行:
1 //设定脚本路径2 public function setScriptPath( $view_directory )//boolean3 {4 var_dump('setScriptPath called with param '.$view_directory);5 return $this->_smarty->setTemplateDir($view_directory);6 }
1 Plugin routerStartup called 2 Plugin routerShutdown called 3 4 string 'setScriptPath called with param F:/zzbserv/apache/htdocs/yaftwo/code/application/modules/test/templates' (length=103) 5 6 Plugin DispatchLoopStartup called 7 Plugin PreDispatch called 8 9 string 'setScriptPath called with param F:/zzbserv/apache/htdocs/yaftwo/code/application/modules/Test/views' (length=99)10 11 -------smarty say hello to you !-------12 Plugin postDispatch called13 Plugin DispatchLoopShutdown called
说明:下边代码块第4行的输出是因为那里我们重写了视图接口,在构造函数中调用了此函数;
但是第9行的输出则是在执行action时的自动调用输出,根据传入的实参可以知道这是yaf自己调用的(现在我还不知道如何去阻止这个调用)
②注意重写接口的diaplay();函数时,要手动拼接上模版文件的绝对路径,这里再贴一遍代码:
1 //显示模版2 public function display( $view_name, $tpl_vars = NULL )//boolean3 {4 $view_path = $this->_script_path.'/'.$view_name;5 $cache_id = empty($tpl_vars['cache_id']) ? '' : $tpl_vars['cache_id'];6 $compile_id = empty($tpl_vars['compile_id']) ? '' : $tpl_vars['compile_id'];7 return $this->_smarty->display($view_path, $cache_id, $compile_id);8 }
说明:注意第4行手动拼接上模版文件的绝对路径(不用管第5,6行代码),这样yaf就可以找到自己定义的模版目录下的模版了,否则:
1 Plugin routerStartup called2 Plugin routerShutdown called3 Plugin DispatchLoopStartup called4 Plugin PreDispatch called5 6 object(SmartyException)[21]7 protected 'message' => string 'Unable to load template file 'test.html'' (length=40)8 ...
最后贴出重写后的用法:
1 <?php2 class IndexController extends Yaf_Controller_Abstract 3 {4 public function aaaAction()5 {6 $this->getView()->assign('var', 'smarty say hello to you !');7 $this->getView()->display('test.html');8 }9 }
另外:
①我在刚接触zend framework的时候,也整合过smarty当时用的是它的全局变量注册功能,我看到yaf的在线手册里也有此类(Yaf_Registry),有用到的可以试试
②Yaf_Controller_Abstract中有个默认执行的init()函数,也可以打打它的注意(*^__^*)
完.
- yaf 整合自定义 视图引擎(smarty)
- Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)
- Yaf整合Smarty模板引擎
- Yaf整合Smarty模板引擎
- Yaf通过composer整合Smarty
- smarty自定义引擎模板
- PHP Yaf 整合 Zend framework 1 和 smarty 2
- 自定义Smarty模板引擎练习
- 视图引擎Smarty的简单使用
- PHP框架一:Smarty视图引擎
- Codeigniter框架整合Smarty引擎DEMO
- 自定义视图引擎
- Zend Framework 整合 Smarty 模板视图
- smarty模板引擎_5-自定义变量调节器
- smarty模板引擎_7-自定义函数
- smarty模板引擎_5-自定义变量调节器
- smarty模板引擎_7-自定义函数
- 模板引擎SMARTY(ZT)
- android 【onMeasure】 introduction
- cvs 导入 mysql
- bash shell 基础
- .bash_profile和.bashrc的什么区别,linux的几个环境配置文件的区别
- hadoop 学习网站
- yaf 整合自定义 视图引擎(smarty)
- PhoneGap cordova platform add android not working while listing Android targets
- 利用JMF获取本地摄像头的两种方法
- 不会放弃,继续努力
- CSS3实践之路(三):CSS3的转换效果(transition)与动画(animation)
- 《苏菲的世界》~哲学是什么?
- 2014读书计划
- 查看 linux flash 分区大小,mtd,nor,nand,partition
- MySQL存储引擎