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代码中来重写视图接口

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视图接口

View Code
 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()函数,也可以打打它的注意(*^__^*)

完.

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266217.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Hibernate(三) - hibernate 表操作-多对多配置

Hibernate 的一对多关联映射 之前在学习 Hibernate 的时候&#xff0c;其实都是单表的操作。在实际的开发当中&#xff0c;比如做一个商城&#xff0c;就需要好多张数据库表&#xff0c;表与表之间是有关系的。之前些做一些关联查询或者是一些基本的查询操作的时候&#xff0c;…

vb treeview 展开子节点_详解最长公共子序列问题,秒杀三道动态规划题目

学算法认准 labuladong后台回复进群一起力扣?读完本文&#xff0c;可以去力扣解决如下题目&#xff1a;1143.最长公共子序列(Medium)583. 两个字符串的删除操作(Medium)712.两个字符串的最小ASCII删除和(Medium)好久没写动态规划算法相关的文章了&#xff0c;今天来搞一把。不…

linux查看数据积压,查看kafka消息队列的积压情况

创建topickafka-topics --create --zookeeper master:2181/kafka2 --replication-factor 2 --partitions 3 --topic mydemo5列出topickafka-topics --list --zookeeper master:2181/kafka2描述topickafka-topics --describe --zookeeper master:2181/kafka2 --topic mydemo5生产…

python 三引号_Python 基础(一):入门必备知识

目录1 标识符2 关键字3 引号4 编码5 输入输出6 缩进7 多行8 注释9 数据类型10 运算符10.1 常用运算符10.2 运算符优先级1 标识符标识符是编程时使用的名字&#xff0c;用于给变量、函数、语句块等命名&#xff0c;Python 中标识符由字母、数字、下划线组成&#xff0c;不能以数…

排序算法:冒泡和快排 摘自网络

冒泡排序&#xff1a; 首先我们自己来设计一下“冒泡排序”&#xff0c;这种排序很现实的例子就是&#xff1a; 我抓一把沙仍进水里&#xff0c;那么沙子会立马沉入水底&#xff0c; 沙子上的灰尘会因为惯性暂时沉入水底&#xff0c;但是又会立马像气泡一样浮出水面&#xff0c…

镭波笔记本安装linux,镭波笔记本windows7旗舰版系统下载与安装教程

镭波笔记本windows7旗舰版系统下载地址以及安装教程有很多盆友询问&#xff0c;今天&#xff0c;我就将镭波电脑下载安装win7旗舰版系统的详细步骤分享给你们,一起来了解一下镭波电脑是如何安装windows7旗舰版。镭波笔记本Windows7旗舰版系统下载&#xff1a;64位Windows7旗舰版…

linux运维和3dmax哪个简单,牛逼运维常用的工具系列-2

劳动最光荣nmonnmon是linux性能监视和分析数据的工具&#xff0c;它的安装很简单&#xff0c;下载解压后&#xff0c;添加可执行权限&#xff0c;即可运行下载解压后&#xff0c;通过文件名可以发现&#xff0c;是多个发行版本的&#xff0c;根据自己的发行版本&#xff0c;然后…

语义分割和实例分割_语义分割入门的一点总结

点击上方“CVer”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达作者&#xff1a;Yanpeng Sunhttps://zhuanlan.zhihu.com/p/74318967本文已由作者授权&#xff0c;未经允许&#xff0c;不得二次转载语义分割目的&#xff1a;给定一张图像&…

linux 音频驱动的流程,Intel平台下Linux音频驱动流程分析

【软件框架】在对要做的事情一无所知的时候&#xff0c;从全局看看系统的拓扑图对我们认识新事物有很大的帮助。Audio 部分的驱动程序框架如下图所示&#xff1a;这幅图明显地分为 3 级。上方蓝色系的 ALSA Kernel 整体属于Linux Kernel&#xff0c;是原生Linux 操作系统的一部…

Windows Server 2008 R2Cisco2960 配置Radius服务 实现802.1x认证 实战

实战配置Windows Server 2008 R2 Radius服务 与Cisco 2960 实现 802.1x认证实验拓扑1.Radius服务器 安装 dc 域名 wjl.com &#xff0c;和ca 安装步骤不再详解2.安装完ca之后&#xff0c;打开MMC 添加计算机证书&#xff0c;查看个人-证书里面有没有ca颁发给计算机的证书&…

linux文件编程(3)—— main函数传参、myCp(配置成环境变量)、修改配置文件、将整数和结构体数组写到文件

参考&#xff1a;linux文件编程&#xff08;3&#xff09;—— 文件编程的简单应用&#xff1a;myCp、修改配置文件 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-09 23:45:05 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115209404 …

linux 修改文件名_Linux常用命令

Linux下一切皆文件查看型ls 查看当前文件夹内容 选项 -a 查看隐藏文件 -l 查看文件详细信息pwd 查看当前所在路径su 切换用户cat /etc/passwd 查看当前系统的用户cat 文件 查看文件内容选项 -n 加上编号 -E 每行末尾加上$ifconfig 查看网卡名&#xff0c;IP地址等网络信息route…

c语言mfc弹出窗口函数,CMFCDesktopAlertWnd实现桌面弹出消息框

1.创建一个CMFCDesktopAlertWnd指针CMFCDesktopAlertWnd* pPopup new CMFCDesktopAlertWnd;2.设置参数pPopup->SetAnimationType((CMFCPopupMenu::ANIMATION_TYPE) 2);pPopup->SetAnimationSpeed(100);pPopup->SetTransparency((BYTE)128);pPopup->SetSmallCaptio…

linux文件编程(2)——系统文件描述符、动静态文件、块设备介绍

参考&#xff1a;linux文件编程&#xff08;2&#xff09;——文件操作原理简述之文件描述符、动静态文件、块设备 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-09 11:14:12 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115209312 目…

java中volatile的使用方式

2019独角兽企业重金招聘Python工程师标准>>> 转载地址&#xff1a; http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html 转载于:https://my.oschina.net/wangfree/blog/122664

linux文件编程(1)—— open、write、read、lseek、阻塞问题(ps文件操作/文件描述符/重定向原理/缓冲区/标准错误)

参考&#xff1a;linux文件编程&#xff08;1&#xff09;—— 常用API之open、write、read、lseek 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-08 22:19:28 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115209134 【Linux】文件操…

linux文件编程(4)—— 用ANSIC标准C库函数进行文件编程:fopen、fread、fwrite、fseek

参考&#xff1a;linux文件编程&#xff08;5&#xff09;—— 用ANSIC标准中的C库函数进行文件编程 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-11 11:58:25 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115209680 部分参照&#…

swig封装 c语言函数到python库,python swig 调用C/C++接口

转载&#xff1a;https://www.cnblogs.com/dda9/p/8612068.html当你觉得python慢的时候&#xff0c;当你的c/c代码难以用在python上的时候&#xff0c;你可能会注意这篇文章。swig是一个可以把c/c代码封装为python库的工具。(本文封装为python3的库)文章结构整体看封装只使用py…

Java学习---面试基础知识点总结

Java中sleep和wait的区别① 这两个方法来自不同的类分别是&#xff0c;sleep来自Thread类&#xff0c;和wait来自Object类。sleep是Thread的静态类方法&#xff0c;谁调用的谁去睡觉&#xff0c;即使在a线程里调用b的sleep方法&#xff0c;实际上还是a去睡觉&#xff0c;要让b线…

使用NPOI和委托做EXCEL导出

首先&#xff0c;在用NPOI导出时&#xff0c;学习了邀月这篇文章NPOI根据Excel模板生成原生的Excel文件实例&#xff0c;在这里先行谢过了。 本篇文章在邀月的基本上&#xff0c;做了一些小的改动&#xff0c;加上委托的机制。因为在做导出时&#xff0c;加载模板&#xff0c;下…