Zend Framework整合Smarty

基于上一篇所讲的多模块结构进行。
1.下载Smarty,然后复制到library目录下
2.通过插件来控制Smarty模板所在目录,具体的结构如下:

自定义插件目录结构

整合Smarty时自定义插件目录结构

3. 通过插件来控制目录
App->controller->plugin->smarty.php  :

1class App_Controller_Plugin_Smarty extendsZend_Controller_Plugin_Abstract
2{
3    public function preDispatch(Zend_Controller_Request_Abstract$request)
4    {
5        $module $request->module;  //得到当前模块
6        $view = Zend_Registry::get('smarty');
7        $view->setBasePath(APPLICATION_PATH.'/modules/'.$module); //指定当前模块目录
8    }
9}

App->View->Smarty.php :

01class App_View_Smarty extends Smarty
02{
03    public function __construct($extraParams array())
04    {
05        $this->Smarty();
06        foreach ($extraParams as $key => $value){
07            $this->$key $value;
08        }
09    }
10 
11    public function setBasePath($path)
12    {
13        $path = rtrim($path'/\\') . DIRECTORY_SEPARATOR;
14        $this->template_dir = $path 'views/scripts';
15        $this->compile_dir  = $path 'views/template_c';
16    }
17}

4.修改配置文件,添加Smarty设置:
[smarty]
left_delimiter=”{{”
right_delimiter=”}}”
caching=0
5.项目入口文件对项目进行设置和启动:

01require_once 'Zend/Loader/Autoloader.php';//设置自动加载类
02Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(TRUE);
03//读取配置文件信息
04$config=newZend_Config_Ini(APPLICATION_PATH.'/configs/application.ini',null, true);
05Zend_Registry::set('config',$config);
06//调用Smarty插件,并进行设值
07$smarty new App_View_Smarty($config->smarty->toarray());
08Zend_Registry::set('smarty'$smarty);
09 
10$frontController = Zend_Controller_Front::getInstance();
11$frontController->setControllerDirectory(APPLICATION_PATH."/modules/default/controllers",'default')
12                    ->setControllerDirectory(APPLICATION_PATH."/modules/admin/controllers",'admin')
13                    ->setModuleControllerDirectoryName("controllers")
14                    ->addModuleDirectory(APPLICATION_PATH.'/modules')
15                    ->setDefaultModule('default')
16                    ->setParam('noViewRenderer', true)
17                    ->setParam('noErrorHandler', true)
18                    ->setParam('useDefaultControllerAlways', true)
19                    ->registerPlugin(new App_Controller_Plugin_Smarty())
20                    ->throwExceptions(true)
21                    ->dispatch();

6.使用并测试成果:
在Default/Controllers/IndexController.php里面添加:

1public function indexAction()
2{
3$this->smarty->assign('index_content','asdfasdfasdf');
4$this->smarty->display('index/index.phtml');
5}

在Default/Views/Scripts/Index/index.phtml里面添加:

1IndexAction传递过来的内容:{{$index_content}}

运行成功,说明没有问题,可以成功显示传过来的值。

转载于:https://my.oschina.net/tenking/blog/32562

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值