zend框架混合使用zend视图和smarty视图

zend框架混合使用zend视图和smarty视图

为了自由使用视图,实践摸索出一套办法。

zend自带了php视图,后缀是phtml,还有一些常用的帮助类等,假如分模块,结构类似

.
|-- application
| |-- configs
| | `-- application.ini
| |-- modules/
| | |-- bbs
| | | |-- controllers
| | | | |-- BbsapplyController.php
| | | | |-- BbstopicController.php
| | | | `-- IndexController.php
| | | |-- models
| | | `-- views
| | | |-- helpers
| | | |-- scripts
| | | `-- templates
| | | |-- Bbsapply
| | | | |-- index.tpl
| | | | `-- view.tpl
| | | |-- Bbstopic
| | | | |-- index1.tpl
| | | | |-- index2.tpl
| | | | `-- index.tpl
| | | `-- index
| | | `-- index.tpl
| | `-- default
| | |-- controllers
| | | |-- IndexController.php
| | | |-- NewsController.php
| | |-- models
| | `-- views
| | |-- helpers
| | |-- scripts
| | | `-- index
| | | |-- index.phtml
| | | `-- index2.phtml
| | `-- templates
| | `-- news
| | |-- category.tpl
| | `-- index.tpl
| `-- templates
| |-- common1.tpl
| |-- common2.tpl
| `-- common3.tpl
|-- library
| |-- Lib1.php
| |-- Lib2.php
| |-- Dao
| | |-- Lib1.php
| | `-- Lib2.php
| |-- Smarty
| | |-- Config_File.class.php
| | |-- Smarty.class.php
| | |-- Smarty_Compiler.class.php
| | |-- Templater.php
| | |-- debug.tpl
| | |-- internals
| | | |-- core.assemble_plugin_filepath.php
| | | |-- core.assign_smarty_interface.php
| | | `-- core.write_file.php
| | `-- plugins
| | |-- block.textformat.php
| | |-- compiler.assign.php
| | |-- function.assign_debug_info.php
| | `-- shared.make_timestamp.php
| |-- Templater
| | `-- plugins
| | |-- function.pagelinks.php
| | |-- function.wysiwyg.php
| | `-- modifier.title1.php
| `-- Zend
| |-- Acl
| |-- Amf
| |-- Auth
| |-- Barcode
| |-- ......
| `-- ......
|-- script
| `-- migrate
| `-- script1.php
`-- documentroot
|-- design
| |-- demo1.html
| `-- demo2.html
|-- images
| |-- bg.png
| |-- index.png
| |-- blank.gif
| |-- captcha.png
| `-- small.png
|-- css
| |-- 1.css
| `-- 2.css
|-- js
| `-- lib
| |-- jquery-1.4.2.min.js
| `-- jquery-ui-1.8.6.custom.min.js
`-- index.php


4个根目录的父目录由版本管理同步,
application 框架程序
library 类库,需要放在php.ini的include_path里,其中含了smarty和Zend框架的类库。
script 一些脚本
documentroot apache的文档根目录

把smarty的模板根目录定位到application,主要是为了有一些smarty公用的模板有地方可放。

为方便起见,zend视图完全按照zend的要求,连后缀都不改,目录结构都不改。
zend视图默认的是"模块名/views/scripts/控制器名/动作名.phtml"
smarty模板则相应使用"模块名/views/templates/控制器名/动作名.tpl"

假设有两个模块,缺省模块和论坛的bbs模块,在目录中可以看出,缺省模块的index控制器使用了zend视图,别的统统使用smarty视图。

在documentroot/index.php里有如下片段

defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

//创建前端控制器对象,
$front_controller = Zend_Controller_Front::getInstance();
$front_controller->addModuleDirectory(APPLICATION_PATH . '/modules');
//分发
$front_controller->dispatch();




[color=red]如果使用zend视图,则控制器类什么都不做。[/color]

在需要使用smarty的控制器里,这样

//缺省模块的新闻控制器
class NewsController extends Zend_Controller_Action
{
function init(){
$viewRenderer = $this->getHelper('viewRenderer');

$viewRenderer->setView(new Smarty_Templater())
->setViewBasePathSpec(APPLICATION_PATH )
->setViewScriptPathSpec('modules/:module/views/templates/:controller/:action.:suffix')
->setViewSuffix('tpl')
->initView();
$this->view = $this->_helper->viewRenderer->view;

parent::init();
}

/**
* 缺省模块的新闻控制器的index行为,使用samrty模板
*/
public function indexAction()
{

}


就这样,整个世界都清净了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值