using zf and smarty

http://www.neatcn.com/show-692-1.shtml

 

使用zend framework开发时,可以采用第三方模版,比如smarty,在网上找了很多资料,一般来说是两种

1、扩展view

2、使用Zend_Registry,在初始化的时候加载smarty,然后在输出的时候使用Zend_Registry::get('smarty')->display();

使用第二种方式的话,我当然是没有什么说的了。我这里说的是使用第一种方案。

在第一种方案中,官方有例子,页面地址为:http://framework.zend.com/manual/en/zend.view.scripts.html,写上一个类,调用Zend_View_Interface,写上相同的函数就可以了。。

官方有源码,我这里就不贴了。我把官方的例子写一下:

 

PHP代码
  1. //Example 1. In initView() of initializer.   
  2. $view  =  new  Zend_View_Smarty( '/path/to/templates' );  
  3. $viewRenderer  =  
  4.     new  Zend_Controller_Action_HelperBroker::getStaticHelper( 'ViewRenderer' );  //使用此例子时,请将new去掉,静态方法不需要new
  5. $viewRenderer ->setView( $view )  
  6.              ->setViewBasePathSpec($view ->_smarty->template_dir)  
  7.              ->setViewScriptPathSpec(':controller/:action.:suffix' )  
  8.              ->setViewScriptPathNoControllerSpec(':action.:suffix' )  
  9.              ->setViewSuffix('tpl' );  
  10.   
  11. //Example 2. Usage in action controller remains the same...   
  12. class  FooController  extends  Zend_Controller_Action  
  13. {  
  14.     public   function  barAction()  
  15.     {  
  16.         $this ->view->book   =  'Zend PHP 5 Certification Study Guide' ;  
  17.         $this ->view->author =  'Davey Shafik and Ben Ramsey'   
  18.     }  
  19. }  
  20.   
  21. //Example 3. Initializing view in action controller   
  22. class  FooController  extends  Zend_Controller_Action  
  23. {  
  24.     public   function  init()  
  25.     {  
  26.         $this ->view   =  new  Zend_View_Smarty( '/path/to/templates' );  
  27.         $viewRenderer  =  $this ->_helper->getHelper( 'viewRenderer' );  
  28.         $viewRenderer ->setView( $this ->view)  
  29.                      ->setViewBasePathSpec($view ->_smarty->template_dir)  
  30.                      ->setViewScriptPathSpec(':controller/:action.:suffix' )  
  31.                      ->setViewScriptPathNoControllerSpec(':action.:suffix' )  
  32.                      ->setViewSuffix('tpl' );  
  33.     }  

以上是官方的example。不过,如果按第一个例子测试,是会出错的。。。

 

请看第4行。。。静态方法居然用了new。(应该是粗心吧。不过我昨天是直接复制的,死活报错,也没有仔细看,丢人啊)写这篇 文章,主要也就是提醒一下,这个例子有点问题。

顺便说一下,由于官方的例子里,是把$_smarty写成了protected,那么,其实在外面是不能够被直接引用的。要么写一个__get方法,要么,把属性改为public吧

不过,在使用smarty后,你会发现,你原来的layout功能不能完全使用了,为什么呢?因为,在原来的layout里面,代码都是类似于这样:

 

PHP代码
  1. <?php  
  2. $this ->layout()->title;  
  3. ?>  

大致是这样的代码,这个,可不能用在smarty中。虽然在smarty中也可以用标签来调用PHP代码,但这毕竟不是一个好办法。

 

不过还好,又有牛人写了一个很牛叉的例子。LOOK:http: //anders.tyckr.com/2008/03/12/implementing-zend-layout-and-smarty-using-zend -framework-mvc/,不过这个例子我还没有全部看完,先贴上来。以后慢慢看,应该会用在项目中吧?

不然,我就用不了layout了,除非我放弃这个东西。。哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值