yaf(5) smarty
2013年4月6日 13:41:37
参考:
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 //整合smarty 11 $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 //smarty 2 public function _initSmarty(Yaf_Dispatcher $dispatcher) 3 { 4 var_dump('@@@@@@@@@'); 5 }
结果:
1 string '@@@@@@@@@' (length=9) 2 3 Plugin routerStartup called 4 Plugin routerShutdown called 5 Plugin DispatchLoopStartup called 6 Plugin PreDispatch called 7 Plugin postDispatch called 8 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 called 2 Plugin routerShutdown called 3 Plugin DispatchLoopStartup called 4 Plugin PreDispatch called 5 -------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视图接口
1 <?php 2 /* 3 * 自定义视图类接口,用来封装smarty共yaf全局使用 4 * 必须实现接口定义的5个函数 5 */ 6 class Smarty_Adapter implements Yaf_View_Interface 7 { 8 public $_smarty; 9 private $_script_path; 10 11 public function __construct() 12 { 13 require 'Smarty.class.php'; 14 $this->_smarty = new Smarty; 15 $this->_smarty->left_delimiter = "<{"; 16 $this->_smarty->right_delimiter = "}>"; 17 $this->_smarty->setCompileDir(ROOT.'/templates_c');//编译目录 18 $this->_smarty->setCacheDir(ROOT.'/cache');//缓存目录 19 //根据不同的模块设置不同的模版路径 20 $dispatcher = Yaf_Dispatcher::getInstance(); 21 $arrRequest = $dispatcher->getRequest(); 22 if (empty($arrRequest->module)) { 23 $this->_script_path = APP.'/templates'; 24 $this->setScriptPath(APP.'/templates'); 25 } else { 26 $strModuleName = strtolower($arrRequest->module); 27 $this->_script_path = APP.'/modules/'.$strModuleName.'/templates'; 28 $this->setScriptPath(APP.'/modules/'.$strModuleName.'/templates'); 29 } 30 } 31 32 //返回要显示的内容 33 public function render( $view_name , $tpl_vars = NULL )//string 34 { 35 $view_path = $this->_script_path.'/'.$view_name; 36 $cache_id = empty($tpl_vars['cache_id']) ? '' : $tpl_vars['cache_id']; 37 $compile_id = empty($tpl_vars['compile_id']) ? '' : $tpl_vars['compile_id']; 38 return $this->_smarty->fetch($view_path, $cache_id, $compile_id, false);//返回应该输出的内容,而不是显示 39 } 40 41 //显示模版 42 public function display( $view_name, $tpl_vars = NULL )//boolean 43 { 44 $view_path = $this->_script_path.'/'.$view_name; 45 $cache_id = empty($tpl_vars['cache_id']) ? '' : $tpl_vars['cache_id']; 46 $compile_id = empty($tpl_vars['compile_id']) ? '' : $tpl_vars['compile_id']; 47 return $this->_smarty->display($view_path, $cache_id, $compile_id); 48 } 49 50 //模版赋值 51 public function assign( $name, $value = NULL )//boolean 52 { 53 return $this->_smarty->assign($name, $value); 54 } 55 56 //设定脚本路径 57 public function setScriptPath( $view_directory )//boolean 58 { 59 return $this->_smarty->setTemplateDir($view_directory); 60 } 61 62 //得到脚本路径 63 public function getScriptPath()//string 64 { 65 return $this->_script_path; 66 } 67 68 }
注意几点:
① setScriptPath()接口函数内不要写判断模版路径是否存在的代码,因为除了在重写视图接口时会执行这行代码外,yaf还会在"分发请求前"默默的执行这个函数,并且传递给它的实参是yaf默认的视图存放路径:views/actionName,此时如果你的layout中没有这个路径的话,你的程序就会报错
帖代码看看这个成员函数什么时候会被执行:
1 //设定脚本路径 2 public function setScriptPath( $view_directory )//boolean 3 { 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 called 13 Plugin DispatchLoopShutdown called
说明:下边代码块第4行的输出是因为那里我们重写了视图接口,在构造函数中调用了此函数;
但是第9行的输出则是在执行action时的自动调用输出,根据传入的实参可以知道这是yaf自己调用的(现在我还不知道如何去阻止这个调用)
②注意重写接口的diaplay();函数时,要手动拼接上模版文件的绝对路径,这里再贴一遍代码:
1 //显示模版 2 public function display( $view_name, $tpl_vars = NULL )//boolean 3 { 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 called 2 Plugin routerShutdown called 3 Plugin DispatchLoopStartup called 4 Plugin PreDispatch called 5 6 object(SmartyException)[21] 7 protected 'message' => string 'Unable to load template file 'test.html'' (length=40) 8 ...
最后贴出重写后的用法:
1 <?php 2 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(5) smarty相关推荐
- php yaf smarty,Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)
Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty) 来源:互联网 作者:佚名 时间:2015-08-06 07:55 对完成某个任务进行计时可使用progress_tim ...
- yaf框架整合smarty
yaf的版本为3.3.2 百度出来的教程可能因为版本原因,均会报错: Fatal error: Declaration of Smarty_Adapter::getScriptPath() must ...
- php 5.5.12 yaf,yaf框架教程(5)- yaf框架的启动文件
yaf框架的启动文件又被称为引导层,一般放在应用目录(application)下,主要作用是在框架启动时,初始化设置,注册插件,加载全局的方法等. 在Bootstrap类中, 所有以_init开头的方 ...
- php yaf twig,yaf-example
其实很早之前就接触yaf了,但只是学习学习,没有深入的去了解和使用,但由于最近在重构一个项目,而重构所用到的框架就是yaf框架,正好趁此机会好好地使用了下yaf框架.本篇文章其实主要给一个yaf的例子 ...
- php 鸟哥 配置文件,鸟哥出品的高性能php框架YAF的安装和配置
Yaf(Yet Another Freamwork)框架是中国国内教父级php程序员鸟哥的主力作品之一, 在php.net有鸟哥(惠新宸)的大名, 可谓国之骄傲.Yaf的运行并非堆积php类文件或者写 ...
- php yaf 模板引擎,yaf-example
其实很早之前就接触yaf了,但只是学习学习,没有深入的去了解和使用,但由于最近在重构一个项目,而重构所用到的框架就是yaf框架,正好趁此机会好好地使用了下yaf框架.本篇文章其实主要给一个yaf的例子 ...
- php-yaf,Yaf框架安装指南
这篇文章主要介绍了关于Yaf框架安装指南 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 说起PHP框架,很多人的印象都停留在一个由PHP实现的基于MVC的各种功能组合的代码包.极少有 ...
- php yaf框架模块化,YAF框架组成部分
一.架构总览 YAF是经典的MVC架构,主要组件如下: (1)应用 YAF系统架构与生命周期的对象,由系统类Yaf_Application(启用命名空间的情况下是Yaf\Application)类完成 ...
- smarty mysql demo_PHP Smarty模版简单使用方法
本文实例讲述了PHP Smarty模版简单使用方法.分享给大家供大家参考,具体如下: Index.php: require('../libs/Smarty.class.php'); $smarty = ...
最新文章
- python编程函数_python编程中函数和参数你必须知道的
- 应用丨其实,你每天都生活在人工智能中
- AAC音频基础知识及码流解析
- log4j.properties 使用
- Settings【学习笔记05】
- 谷歌推出理解神经网络的新方法SVCCA | NIPS论文+代码
- 3.1 Tensorflow基础知识
- Windows Server 2008 R2将tomcat添加进系统服务
- html arm音频播放器,web页面播放arm格式音频
- 幼儿园计算机认识键盘上课教案,认识键盘教案
- Decorate 装饰器应用
- web IDE theia-ide安装
- 《信息安全系统设计基础》第1周问题总结
- Xilinx的FPGA手册中关于如何Booting RFSoCsZynq
- flutter dio网络请求 get post 图片上传
- 【自学笔记】尚硅谷数据结构与算法Chapter 2 稀疏数组和队列
- Eclipse提示功能
- Quartz概念详解
- 软件需求工程 课堂笔记8
- 彻底卸载智能云输入法(在win10家庭版下)