Zend Framework整合Smarty

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

自定义插件目录结构

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

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

1 class 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 :测试

01 class 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.项目入口文件对项目进行设置和启动:ui

01 require_once 'Zend/Loader/Autoloader.php';//设置自动加载类
02 Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(TRUE);
03 //读取配置文件信息
04 $config=newZend_Config_Ini(APPLICATION_PATH.'/configs/application.ini',null, true);
05 Zend_Registry::set('config',$config);
06 //调用Smarty插件,并进行设值
07 $smarty new App_View_Smarty($config->smarty->toarray());
08 Zend_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里面添加:this

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

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

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

运行成功,说明没有问题,能够成功显示传过来的值。插件

相关文章
相关标签/搜索